Learn Java - Variables Cheatsheet - 2
Learn Java - Variables Cheatsheet - 2
Variables
In Java, the boolean primitive data type is used to store boolean result = true;
a value, which can be either true or false .
boolean isMarried = false;
Strings
A String in Java is a Object that holds multiple characters. // Creating a String variable
It is not a primitive datatype.
String name = "Bob";
A String can be created by placing characters between a
pair of double quotes ( " ).
To compare Strings, the equals() method must be used // The following will print "false"
instead of the primitive equality comparator == .
because strings are case-sensitive
System.out.println(name.equals("bob"));
In Java, the int datatype is used to store integer values. int num1 = 10; // positive value
This means that it can store all positive and negative
int num2 = -5; // negative value
whole numbers and zero.
int num3 = 0; // zero value
int num4 = 12.5; // not allowed
In Java, char is used to store a single character. The char answer = 'y';
character must be enclosed in single quotes.
https://www.codecademy.com/learn/learn-java/modules/learn-java-variables/cheatsheet 1/4
1/3/24, 10:12 AM Learn Java: Variables Cheatsheet | Codecademy
Java’s most basic data types are known as primitive data int age = 28;
types and are in the system by default.
The available types are as follows:
int char grade = 'A';
char
boolean
boolean late = true;
byte
long
short byte b = 20;
double
float
long num1 = 1234567;
null is another, but it can only ever store the value
null .
short no = 10;
float k = (float)12.5;
double pi = 3.14;
Static Typing
In Java, the type of a variable is checked at compile time. int i = 10; // type is int
This is known as static typing. It has the advantage of
char ch = 'a'; // type is char
catching the errors at compile time rather than at
execution time.
Variables must be declared with the appropriate data j = 20; // won't compile, no
type or the program will not compile.
type is given
char name = "Lil"; // won't compile,
wrong data type
final Keyword
The value of a variable cannot be changed if the variable // Value cannot be changed:
was declared using the final keyword.
final double PI = 3.14;
Note that the variable must be given a value when it is
declared as final . final variables cannot be changed;
any attempts at doing so will result in an error message.
https://www.codecademy.com/learn/learn-java/modules/learn-java-variables/cheatsheet 2/4
1/3/24, 10:12 AM Learn Java: Variables Cheatsheet | Codecademy
Math Operations
result = a - b; // 10
result = a * b; // 200
result = a / b; // 2
result = a % b; // 0
Comparison Operators
https://www.codecademy.com/learn/learn-java/modules/learn-java-variables/cheatsheet 3/4
1/3/24, 10:12 AM Learn Java: Variables Cheatsheet | Codecademy
Order of Operations
Print Share
https://www.codecademy.com/learn/learn-java/modules/learn-java-variables/cheatsheet 4/4