- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.java
More file actions
Latest commit
81 lines (69 loc) · 2.46 KB
/
Copy pathvariable.java
File metadata and controls
81 lines (69 loc) · 2.46 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
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
JavaVariables
Variablesarecontainersforstoringdatavalues.
InJava, therearedifferenttypesofvariables, forex:
String - storestext, suchas"Hello". Stringvaluesaresurroundedbydoublequotes
int - storesintegers (wholenumbers), withoutdecimals, suchas123or -123
float - storesfloatingpointnumbers, withdecimals, suchas19.99or -19.99
char - storessinglecharacters, suchas'a'or'B'. Charvaluesaresurroundedbysinglequotes
boolean - storesvalueswithtwostates: trueorfalse
typevariableName = value;
Thegeneralrulesfornamingvariablesare:
Namescancontainletters, digits, underscores, anddollarsigns
Namesmustbeginwithaletter
Namesshouldstartwithalowercaseletter, andcannotcontainwhitespace
Namescanalsobeginwith$and_
Namesarecase-sensitive ("myVar"and"myvar"aredifferentvariables)
Reservedwords (likeJavakeywords, suchasintorboolean) cannotbeusedasnames
classvariable{
publicstaticvoidmain(String[] args){
Stringname = "John";
System.out.println(name);
finalintmyNum = 15;
//myNum = 20; // will generate an error: cannot assign a value to a final variable
intmyum = 5;
floatmyFloatNum = 5.99f;
charmyLetter = 'D';
booleanmyBool = true;
StringmyText = "Hello";
Stringname = "John";
System.out.println("Hello " + name);
StringfirstName = "John ";
StringlastName = "Doe";
StringfullName = firstName + lastName;
System.out.println(fullName);
intx = 5;
inty = 6;
System.out.println(x + y);
intx = 5, y = 6, z = 50;
System.out.println(x + y + z);
intx, y, z;q
x = y = z = 50;
System.out.println(x + y + z);
// Good
intminutesPerHour = 60;
// OK, but not so easy to understand what m actually is
intm = 60;
// Student data
StringstudentName = "John Doe";
intstudentID = 15;
intstudentAge = 23;
floatstudentFee = 75.25f;
charstudentGrade = 'B';
// Print variables
System.out.println("Student name: " + studentName);
System.out.println("Student id: " + studentID);
System.out.println("Student age: " + studentAge);
System.out.println("Student fee: " + studentFee);
System.out.println("Student grade: " + studentGrade);
// Create integer variables
intlength = 4;
intwidth = 6;
intarea;
// Calculate the area of a rectangle
area = length * width;
// Print variables
System.out.println("Length is: " + length);
System.out.println("Width is: " + width);
System.out.println("Area of the rectangle is: " + area);
}
}