int, double, char, ...
are primitive (predefined)
data typesString
is (however) an object, like everything elseTyp | Size | Domain | Example |
---|---|---|---|
boolean | 1 | {true, false} | |
short | 2 | ||
int | 4 | [−231, 231−1] = | |
long | 8 | [−263, 263−1] | |
float | 4 | [1.40239846 ⋅ 10–45, 3.40282347 ⋅ 1038] | |
double | 8 | [4.94065645841246544 ⋅ 10–324, 1.79769131486231570 ⋅ 10308] |
short
/ long
for specialized applications onlyfloat
not used anymore, standard isdouble
for floating point operationsint
/double
) used for
memory addressing by 32/64 bit processors,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); } } }
String
class java.lang.String String
concatenation with +
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
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
int[] array = new int[n];
int b = array[k];
int[] array = {1, 2, 3};
int b = array.length;
int[][] array = new int[n][m];
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]); } }
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
training suggestion
write a code for calculating the scalar product
between two vectors
and for matrix multiplication
int[][] matrix = new int[n][m];
matrix[i][j]
int[][] matrix = new int[][] {{1, 2, 3}, {4, 5, 6}};
write a code for evaluating the trace of a given matrix
Symbol | Description | Examples |
---|---|---|
= | 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 |
import java.lang.Math;
static final double PI; static final double E;
static int abs(int x);
static int min(int x, int y); static int max(int x, int y);
static double sin(double x); static double cos(double x); static double tan(double x); static double asin(double x); static double acos(double x); static double atan(double x); static double hsin(double x); static double hcos(double x); static double htan(double x);
static double sqrt(double x); static double cbrt(double x);
static double exp(double x);
static double pow(double x, double y);
static double log(double a); static double log10(double a);
static double round(double a); static long round(double a);