0% found this document useful (0 votes)
2 views7 pages

05. Variables and String Method

The document provides a comprehensive overview of Java programming concepts, focusing on data types such as boolean, String, int, char, and double, along with their usage and syntax. It covers static typing, the final keyword, math operations, comparison operators, and various string methods like length(), indexOf(), and concat(). Additionally, it explains compound assignment, increment/decrement operators, and the order of operations in expressions.
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)
2 views7 pages

05. Variables and String Method

The document provides a comprehensive overview of Java programming concepts, focusing on data types such as boolean, String, int, char, and double, along with their usage and syntax. It covers static typing, the final keyword, math operations, comparison operators, and various string methods like length(), indexOf(), and concat(). Additionally, it explains compound assignment, increment/decrement operators, and the order of operations in expressions.
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/ 7

Firefox about:srcdoc

Cheatsheets / Learn Java

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.

1 of 4 7/3/25, 17:50
Firefox about:srcdoc

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
byte b = 20;
• short
• 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

�nal 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.

2 of 4 7/3/25, 17:50
Firefox about:srcdoc

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 , int a = 20;


double 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 .

3 of 4 7/3/25, 17:50
Firefox about:srcdoc

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.

Print Share

4 of 4 7/3/25, 17:50
Firefox about:srcdoc

Cheatsheets / Learn Java

String Methods

length() String Method in Java

In Java, the length() string method returns the total String str = "Codecademy";
number of characters – the length – of a String .

System.out.println(str.length());
// prints 10

indexOf() String Method in Java

In Java, the indexOf() string method returns the String str = "Hello World!";
�rst occurence of a character or a substring in a
String . The character/substring that you want to �nd
System.out.println(str.indexOf("l"));
the index of goes inside of the () .
If indexOf() cannot �nd the character or substring, // prints 2
it will return -1.

System.out.println(str.indexOf("Wor"));
// prints 6

System.out.println(str.indexOf("z"));
// prints -1

concat() String Method in Java

In Java, the concat() string method is used to String s1 = "Hello";


append one String to the end of another String . String s2 = " World!";
This method returns a String representing the text of
the combined strings.
String s3 = s1.concat(s2);
// concatenates strings s1 and s2

System.out.println(s3);
// prints "Hello World!"

1 of 3 7/3/25, 17:55
Firefox about:srcdoc

String Method equals() in Java

In Java, the equals() string method tests for equality String s1 = "Hello";
between two String s. String s2 = "World";
equals() compares the contents of each String . If
all of the characters between the two match, the
method returns true . If any of the characters do not System.out.println(s1.equals("Hello"));
match, it returns false . // prints true
Additionally, if you want to compare two strings without
considering upper/lower cases, you can use
System.out.println(s2.equals("Hello"));
.equalsIgnoreCase() .
// prints false

System.out.println(s2.equalsIgnoreCase("w
orld"));
// prints true

charAt() String Method in Java

In Java, the charAt() string method returns the String str = "This is a string";
character of a String at a speci�ed index. The index
value is passed inside of the () , and should lie
System.out.println(str.charAt(0));
between 0 and length()-1 .
// prints 'T'

System.out.println(str.charAt(15));
// prints 'g'

toUpperCase() and toLowerCase() String Methods

In Java, we can easily convert a String to upper and String str = "Hello World!";
lower case with the help of a few string methods:
• toUpperCase() returns the string value
converted to uppercase.
String uppercase = str.toUpperCase();
• toLowerCase() returns the string value // uppercase = "HELLO WORLD!"
converted to lowercase.

String lowercase = str.toLowerCase();


// lowercase = "hello world!"

Print Share

2 of 3 7/3/25, 17:55
Firefox about:srcdoc

3 of 3 7/3/25, 17:55

You might also like