Java Chapter 2
Java Chapter 2
== Equal to x == y
!= Not equal x != y
&& Logical and Returns true if both statements are true x < 5 && x < 10
◼ Real programmers use a calculator! We'll just have simple values in exams so you
don't need a calculator and practice the basics
AND Boolean Operator
• """
• The & operator compares each bit and set
it to 1 if both are 1, otherwise it is set to 0:
• 6 = 0000000000000110
• 3 = 0000000000000011
• --------------------
• 2 = 0000000000000010
OR Boolean Operator
• print(6 | 3)
• """
• The | operator compares each bit and set it to 1
if one or both is 1, otherwise it is set to 0:
• 6 = 0000000000000110
• 3 = 0000000000000011
• --------------------
• 7 = 0000000000000111
XOR
False True
Bitwise Operators: NOT
• print(~3)
• """
• The ~ operator inverts each bit (0
becomes 1 and 1 becomes 0).
• String is the type of objects that can store the sequence of characters enclosed by double
quotes and every character is stored in 16 bits. A string acts the same as an array of
characters.
Java Strings
System.out.println(firstName.concat(lastName));
}
String Concatenation
String x2 = "10";
String y2 = "20";
String z2 = x2 + y2;
System.out.println(z1);
System.out.println(z2);
}}
String Comparison
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));
}}
Substring
}}
Checking if a String Contains Another String
System.out.println(str.contains("Java"));
System.out.println(str.contains("Python"));
}}
Trimming Whitespace
System.out.println(str.trim());
}}
Replacing Characters
System.out.println(replacedStr);
}}