Lesson1 and Lesson2 Class Notes
Lesson1 and Lesson2 Class Notes
1
Lesson – 2
Data Types
1. Primitive Types
ReadLine()
3. Scanner - JDK 5.0 ( Mostly recommended) - Read as String, Int, Float
BufferedReader ob = new BufferedReader(new
InputStreamReader(System.in));
Scanner ob = new Scanner(System.in);
int x =ob.nextInt();
2
int x = 10;
int y = 5;
int z = 0;
if( x>y)
z = x;
else
x = y;
Ternary operator(?:) z = (x>y)?x:y;
Math class is called as Utility class. You cannot create object for the Math class due to
private constructor and not inherit from Math class due to final class. It’s not immutable
class.
Math ob = new Math(); //will get compilation error
It includes static fields and static methods.
Automatic Promotion
byte b1 = 10;
byte b2 = 11;
byte b3 = (byte) (b1+b2); // or int b3 = b1 + b2;
Java String
String x = “Java”;
// Within Double quotes – sequence of Character
3
Strings are Immutable
Try to modify the x value
X = x + “Programming”;
If modify the value it does not modify the original, instead of modifying it will create a
new String with the value of
“Java Programming”
// How to declare strings
String x = “Java” ; // String Literal
String x1 = new String(“Java”); String object
If string literals are equal or not compare If string objects are equal or not compare
using == using equals() method
String comparison using == checks the String comparison using equals() checks
references are same the contents are same
4
String x1 = new String(“Java”); // 00AB
String x1 = new String(“Java”); // 0012
5
Array Copy Approaches
Give inputs for the Command line Arguments, main method args[] in IntelliJ IDEA,
1. Right Click, Select More Run/Debug > Modify Run Configuration
6
Make sure your Class Name, and give input arguments separated by space like one two
three, then click OK.
When to use Arrow (->) Syntax: Use this for simple cases where the right-hand side
of the arrow is a single expression or a straightforward block of code. It makes the
code more concise and readable.
switch (variable) {
case value1 -> expression1;
case value2 -> expression2;
// ...
default -> defaultExpression;
}
When to use yield Keyword: Use this when you need to execute multiple statements
or perform more complex logic within a case. The yield keyword is used to return a
value from the case block.
7
The yield statement must be the last statement in each case block. It specifies the
value to be returned from the switch expression for that case.
switch (variable) {
case value1->{
// multiple statements
yield expression1;
}
case value2->{
// multiple statements
yield expression2;
}
// ...
Default-> {
// multiple statements
yield defaultExpression;
}
}
HW Problem – 3 - Hints
"231A,Light Bulb,123,Wilco,1.75:"
"113D,Hairbrush,19,Aamco,3.75:"
231A Light Bulb 123 Wilco 1.75
113D Hairbrush 19 Aamco 3.75
String.Split()
Row Split use delimeter (:)
Column split use delimeter(,)