Chapter 1: Variables & Data Types
Keywords in Java
Keywords are words which are reserved and used by the Java Compiler. They cannot be used as an Identifier.
Access Modifiers
Data Types
Control Flow
Class & Object
Note: Go to docs.oracle.com for a comprehensive list of all Java keywords!
Variables in Java
A variable is a container that holds data that can be changed during the execution of a program.
Variable Naming Rules:
✅ Valid Names
agestudentName_count$pricenumber1❌ Invalid Names
2number (starts with digit)class (keyword)my-name (contains hyphen)my name (contains space)@value (invalid character)Data Types in Java
Data types specify the different sizes and values that can be stored in the variable.
Java is statically typed - variables must be declared before use.
Primitive Data Types
There are 8 primitive data types supported by Java:
| Data Type | Size | Range | Default Value |
|---|---|---|---|
byte |
1 byte | -128 to 127 | 0 |
short |
2 bytes | -32,768 to 32,767 | 0 |
int |
4 bytes | -2,147,483,648 to 2,147,483,647 | 0 |
long |
8 bytes | -2^63 to 2^63-1 | 0L |
float |
4 bytes | 3.4e-038 to 3.4e+038 | 0.0f |
double |
8 bytes | 1.7e-308 to 1.7e+308 | 0.0d |
char |
2 bytes | 0 to 65,535 | '\u0000' |
boolean |
1 bit | true or false | false |
How to choose data types for our Variables
Primitive Data Types
Integral (int)
byte, short, int, longFloats (Decimal)
float(f), double(d)Char, Boolean
char, boolean(true/false)In order to choose the data type we first need to find the type of data we want to store. After that we need to analyze the Min & Max Value we might use.
Literals
A constant value which can be assigned to the variable is called a literal.
Types of Literals:
101→ Integer Literal10.1f→ Float Literal10.1→ Double Literal (default type for decimals)'A'→ Character Literaltrue→ Boolean Literal"Harry"→ String Literal
Reading Data from the Keyboard
In order to read data from the keyboard, Java has a Scanner class.
Scanner class has a lot of methods to read data from the keyboard.
Scanner Setup:
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read different types of input
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your salary: ");
double salary = scanner.nextDouble();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
scanner.close();
}
}
Scanner Methods:
| Method | Description | Example |
|---|---|---|
nextInt() |
Reads an integer | int num = scanner.nextInt(); |
nextDouble() |
Reads a double | double price = scanner.nextDouble(); |
nextFloat() |
Reads a float | float rate = scanner.nextFloat(); |
next() |
Reads a single word | String word = scanner.next(); |
nextLine() |
Reads entire line | String line = scanner.nextLine(); |
nextBoolean() |
Reads a boolean | boolean flag = scanner.nextBoolean(); |
Practice Set
Exercise 1.1: Student Percentage Calculator
Problem: Write a program to calculate percentage of a given student in CBSE board exam. His marks from 5 subjects must be taken as input from the keyboard. (Marks are out of 100).
Chapter 1 - Practice Set
1. Sum of Three Numbers
Write a program to sum three numbers in Java.
2. CGPA Calculator
Write a program to calculate CGPA using marks of three subjects (out of 100).
3. Greeting Program
Write a Java program which asks the user to enter his/her name and greets them with "Hello <name>, have a good day" text.
4. Unit Conversion
Write a Java program to convert Kilometers to miles.
5. Number Type Detection
Write a Java program to detect whether a number entered by the user is integer or not.