Spaces:
Running
Running
File size: 2,795 Bytes
413f2e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | Java's basic types
Type Saved information Number range
byte integer 1 byte
short integer 2 bytes
char character One Unicode-system character
int integer 4 bytes
long integer 8 bytes
float floating point number (i.e decimal number) 4 bytes
double floating point number 8 bytes
eg
public class Example {
public static void main(String[] args) {
String name = "Mike Madeup";
int age = 23;
double height = 171.25;
}
}
eg
// boolean (IN LOWERCASE) - true, false
public class Example {
public static void main(String[] args) {
boolean True = true;
boolean False = false;
boolean first_larger = 10 > 3;
System.out.println(first_larger);
}
}
Output:
true
eg
public class Example {
public static void main(String[] args) {
int first = 23;
int second = 10;
int multiply = first * second;
int sum = first + second;
int largeNum = (first + second) * 100;
System.out.println(multiply);
System.out.println(sum);
System.out.println(largeNum);
}
}
Output:
230
33
3300
eg
//If there are several different types involved in an expression, the type of the result is determined by the "largest" type, for example:
public class Example {
public static void main(String[] args) {
int integer = 10;
double float = 5.0;
System.out.println(integer * 2);
System.out.println(float * 2);
System.out.println(integer * 2.0);
System.out.println(integer + integer);
System.out.println(integer + float);
}
}
Output:
20
10.0
20.0 // int * float -> float
20
15.0 //int + float -> float
eg
//division
public class Example {
public static void main(String[] args) {
System.out.println(5 / 2);
System.out.println(5 / 2.0);
System.out.println(5.0 / 2);
System.out.println(5.0 / 2.0);
}
}
Output:
2 // int/int -> int
2.5 // int/float -> float
2.5 // float/int -> float
2.5 // float/float -> float
eg
public class Example {
public static void main(String[] args) {
int divided = 10;
int divisor = 4;
// This returns and integer...
System.out.println(divided / divisor);
// Result is wrong here too...
double divide = divided / divisor;
System.out.println(divide);
//...but this works:
System.out.println(divided * 1.0 / divisor);
}
}
Output:
2 // int/int -> int (rounded down)
2.0 // int/int -> int (rounded down) -> then convert to double
2.5 // 'new' double/int -> double
eg
2 * (3 - 1) 4
6/4 1
2 * 3.0 + 1 7.0
2.0 * (3.0 - 1.0) 4.0
2 * 3 + 1 7
2 * (3 + 1) 8
2 * (3.0 + 1) 8.0
6/(2*2.0) 1.5
|