0% found this document useful (0 votes)
21 views

Learn Java - Introduction - Variables Cheatsheet - Codecademy

Learn variables for free

Uploaded by

missphosphorus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Learn Java - Introduction - Variables Cheatsheet - Codecademy

Learn variables for free

Uploaded by

missphosphorus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Cheatsheets / Learn Java: Introduction

Variables

boolean Data Type


In Java, the boolean primitive data type is used to boolean result = true;
store a value, which can be either true or false .
boolean isMarried = false;

Strings
A String in Java is a Object that holds multiple // Creating a String variable
characters. 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 // The following will print "false"
used instead of the primitive equality comparator == .
because strings are case-sensitive
System.out.println(name.equals("bob"));

int Data Type


In Java, the int datatype is used to store integer int num1 = 10; // positive value
values. This means that it can store all positive and
int num2 = -5; // negative value
negative whole numbers and zero.
int num3 = 0; // zero value
int num4 = 12.5; // not allowed

char Data Type


In Java, char is used to store a single character. The char answer = 'y';
character must be enclosed in single quotes.
Primitive Data Types
Java’s most basic data types are known as primitive int age = 28;
data 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 int i = 10; // type is int
time. This is known as static typing. It has the advantage
char ch = 'a'; // type is char
of 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 // Value cannot be changed:
variable 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.

double Data Type


The double primitive type is used to hold decimal double PI = 3.14;
values.
double price = 5.75;
Math Operations
Basic math operations can be applied to int , double int a = 20;
and float data types:
int b = 10;
+ addition
- subtraction
* multiplication int result;
/ division
% modulo (yields the remainder)
result = a + b; // 30
These operations are not supported for other data
types.
result = a - b; // 10

result = a * b; // 200

result = a / b; // 2

result = a % b; // 0

Comparison Operators
Comparison operators can be used to compare two int a = 5;
values:
int b = 3;
> greater than
< less than
>= greater than or equal to boolean result = a > b;
<= less than or equal to // result now holds the boolean value
== equal to
true
!= not equal to
They are supported for primitive data types and the
result of a comparison is a boolean value true or
false .

Compound Assignment Operators


Compound assignment operators can be used to int number = 5;
change and reassign the value of a variable using one
line of code. Compound assignment operators include
+= , -= , *= , /= , and %= . number += 3; // Value is now 8
number -= 4; // Value is now 4
number *= 6; // Value is now 24
number /= 2; // Value is now 12
number %= 7; // Value is now 5
Increment and Decrement Operators
The increment operator, ( ++ ), can increase the value int numApples = 5;
of a number-based variable by 1 while the decrement
numApples++; // Value is now 6
operator, ( -- ), can decrease the value of a variable by
1.
int numOranges = 5;
numOranges--; // Value is now 4

Order of Operations
The order in which an expression with multiple
operators is evaluated is determined by the order of
operations: parentheses -> multiplication -> division ->
modulo -> addition -> subtraction.

Casting
Casting is the process of transforming a value of one // Convert double to int
primitive data type to another.
double numDouble = 12.99;
A double value can be transformed into an int value
using the (int) casting operator, while an int value System.out.println((int)doubleNum); //
can be transformed into a double value using the Prints: 12
(double) casting operator.

// Convert int to double


int numInt = 9;
System.out.println((double)numInt); //
Prints: 9.0

Print Share

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy