Programmierpraktikum




Claudius Gros, SS2012

Institut für theoretische Physik
Goethe-University Frankfurt a.M.

Java - Basic Data Types


primitive data types

Typ Size Domain Example
boolean 1 {true, false}
short 2
int 4 [−231, 231−1] =
long8 [−263, 263−1]
float 4 [1.40239846 ⋅ 10–45, 3.40282347 ⋅ 1038]
double 8 [4.94065645841246544 ⋅ 10–324, 1.79769131486231570 ⋅ 10308]

example: primitive data types

public class PrimitiveDataTypes {

  public static void main(String[] args)
    {

// --- pi with double (16 digits) and float (8 digits)
// --- Math.PI is a double constant
    double double_PI = Math.PI;
    float   float_PI = (float)Math.PI;    // casting double to float
   
    System.out.printf("pi with double: %24.20f\n",double_PI);
    System.out.printf("pi with  float: %24.20f\n",float_PI);
    System.out.printf("%25s ^\n","");   // pointer 
    System.out.println(" ");            // empty line

// --- int and long integer numbers
    int   int_int = 1;
    long long_int = 1;

    for (int i=0; i<34; i++)
      {
       int_int =   int_int * 2;
      long_int =  long_int * 2;
      System.out.printf("i+1, short, int, long: %4d %12d %12d\n",
                         i+1, int_int, long_int);
      }

    }
}

strings


public class ExampleStrings {
  public static void main(String[] args) 
  {

// --- defining and printing
// --- toUpperCase() is a member function of the String class
  String cdde = "cdde";
  System.out.println("abc");
  System.out.println(cdde);
  System.out.println(cdde.toUpperCase());

// --- String concatenation with '+'
  System.out.println("abc" + " " + cdde);

// --- substring() is a member function of the String class
  String c  = "abc".substring(2,3);
  String dd = cdde.substring(1,3);
  System.out.println(" c: " + c);
  System.out.println("dd: " + dd);

// --- valueOf() casts numbers into strings
  System.out.println(String.valueOf(Math.PI));

  } // end of ExampleStrings.main()
} // end of class ExampleStrings

casting vs parsing

import java.util.*;
import java.math.*;

public class CastingDemo {

public static void main(String args[]) {

   double rr = Math.random();
   String ss = String.valueOf(rr);         // converting double into string
   System.out.println("a random number as a string: " + ss);
   System.out.printf( "                   and back: %f\n\n",
              Double.parseDouble(ss));     // parsing
//
   int ii = (int)(Math.random()*100.0);    // casting double into int
   System.out.printf( " random integer and string : %d %s\n",
              ii, String.valueOf(ii));

  }  // end of CastingDemo.main()
}    // end of CastingDemo

arrays

example: arrays

public class ExampleArrays {
  public static void main(String[] args) {

// --- array of integers
    int[] values = {2, 3, 5, 7, 11, 13};
    System.out.println(values[3]);     // 7

// --- array of strings
    String[] colors = {"yellow", "red", "green", "blue"};
    colors[0] = "black";
    for (int i=0; i<colors.length; i++)
      System.out.printf("This is color[%d]: %s \n", i, colors[i]);
    System.out.println(" ");           // empty line

// --- splitting a string, typically used when reading from file
    String inputString = "age height weight";
    String[] words = inputString.split(" ");
    for (int i=0; i<words.length; i++)
      System.out.printf("This is words[%d]: %s \n", i, words[i]);
    System.out.println(" ");           // empty line

// --- array of chars
  char[] chars = "black sun".toCharArray();
    for (int i=0; i<chars.length; i++)
      System.out.printf("This is chars[%d]: %c \n", i, chars[i]);
  }
}

example: matrices

public class ExampleMatrices {
  public static void main(String[] args) 
  {

// --- defining a 3x2 matrix (3 rows and 2 columns)
  String[][] strMatrix = new String[3][2];
  String[] rowZero = { "rowZero[0]", "rowZero[1]"};
  String[] rowOne  = { "rowOne[0]", "rowOne[1]"};
  strMatrix[0] = rowZero;             // setting row [0]
  strMatrix[1] = rowOne;               // setting row [1]

// --- printing matrix
// --- note: row[2] is undefined (null)
  for (int i = 0; i<strMatrix.length; i++)
    for (int j = 0; j<strMatrix[i].length; j++)
      System.out.printf("  strMatrix[%d][%d]:  %s\n",
                         i, j, strMatrix[i][j]);
  System.out.println(" ");

// --- a matrix needs not to be rectangular, may be ragged
  int[][] intMatrix = new int[3][];    // allocate the number of rows
  int[] row_0 = {0,1,2,3,4};
  int[] row_1 = {5,6,7};
  int[] row_2 = {8,9};
  intMatrix[0] = row_0;
  intMatrix[1] = row_1;
  intMatrix[2] = row_2;

  for (int i = 0; i<intMatrix.length; i++)
    {
    System.out.printf("  strMatrix[%d]:",i);
    for (int j = 0; j<intMatrix[i].length; j++)
      System.out.printf(" %d",intMatrix[i][j]);
    System.out.printf("\n");
    }

  } // end of ExampleMatrices.main()
} // end of class ExampleMatrices

exercise: matrix operations

training suggestion
write a code for calculating the scalar product between two vectors
and for matrix multiplication


write a code for evaluating the trace of a given matrix

arithmetic operators

SymbolDescriptionExamples
=assignment int i=5;
+  -addition, subtraction int i=5+3; int i=5-3;
*  /multiplication, integer division int i=5*3; int i=9/3;
%modulo int i=14%3;
+  -unary plus/minus int i=+3; int i=-2;
+=  -=  *=   /=assignment and operation i += 5; // i = i + 5
++   --pre-/postfix increment/decrement i++; // i = i + 1

mathematical functions

import for basic mathematics

import java.lang.Math;