Lecture 2
Lecture 2
Introduction to Programming
Introduction
• Data Types :
• Primitive Data Types: int,double,boolean,char
• Non-Primitive Data Types: String, arrays, classes.
– Basic Operators:
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
Example Code
• Float: Double:
• Size: 4 bytes (32 bits) • Size: 8 bytes (64 bits)
• Precision: Up to 7 decimal • Precision: Up to 15-16
digits decimal digits
• Default value: 0.0f • Default value: 0.0d
• Example: • Example:
float price = 9.99f; double pi =
3.141592653589793;
All primitive data types
Input in Java
// Input
System.out.print("Enter your name: ");
String name = scanner.nextLine();
// Output
System.out.println("Hello, " + name + "!");
System.out.printf("You are %d years old.\n", age);
}
}
Math functions
• Math.abs(x): Returns the absolute value of x (for both integers and floating-point numbers)
• Math.max(x, y) and Math.min(x, y): Return the maximum or minimum of two numbers.
• Math.pow(base, exponent): Returns the value of base raised to the power of exponent.
• Math.sqrt(x): Returns the square root of x
• Math.cbrt(x): Returns the cube root of x
• Math.ceil(x) and Math.floor(x): ceil rounds up to the nearest whole number, while floor
rounds down
• Math.round(x): Rounds x to the nearest integer
• Math.sin(angle):Returns the sine of an angle (in radians).
• Math.cos(angle): Returns the cosine of an angle (in radians).
• Math.PI: Value of π (3.141592653589793).
• Math.E: Value of Euler's number (e ≈ 2.718281828459045).