-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVariables.java
More file actions
39 lines (38 loc) · 1.43 KB
/
Variables.java
File metadata and controls
39 lines (38 loc) · 1.43 KB
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
public class Variables {
public static void main(String[] args) {
// java is strongly typed hence types must be defined in variables/methods
// primitive variables -> Hold simple data values and are not objects.
// byte(number, 1 byte)
byte a = 100;
System.out.println(a);
// short(number, 2 bytes)
short b = 100;
System.out.println(b);
// int(number, 4 bytes)
int c = 100;
System.out.println(c);
// long(number, 8 bytes)
long d = 100;
System.out.println(d);
// float(float number, 4 bytes). - If you want to use float, you will have to cast
float e = (float)100.1; // or shorter way = 100.1f
System.out.println(e);
// double(float number, 8 bytes)
double f = 100.0;
System.out.println(f);
// char(a character, 2 bytes)
char g = 'g';
System.out.println(g);
// boolean(true or false, 1 byte)
boolean h = true;
System.out.println(h);
// non-primitive variables / reference types -> Store reference (memory addresses) to objects rather than the objects themselves
// Include Classes, Interfaces, Arrays and Enumerations
String i = "Sample String";
System.out.println(i);
String i1 = new String("Sample String 2");
System.out.println(i1);
int[] j = {1, 3, 5, 5};
System.out.println(j);
}
}