Java First Program
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Java Comments
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Java single-line Comments
Single-line comments start with two forward slashes (//).
This example uses a single-line comment before a line of code:
Java multi-line Comments
Multi-line comments start with /* and ends with */.
Example
/* The code below will print the words Hello World
to the screen, and it is amazing */ System.out.println(“Hello World”);
Java Variables
Variables are containers for storing data values.
In Java, there are different types of variables, for example:
Variables Declaration and Initialization
- Declaration: This involves specifying the data type and the variable name.
- Initialization: This involves assigning a value to the variable.
// Declarationint a; // Initialization a = 10; // Declaration and Initializationint b = 20;
Types of Variables in Java
- Variables Local Variables
- Instance Variables
- Static (Class)
Local Variables
- Declared inside a method, constructor, or block.
- Created when the method, constructor, or block is entered and destroyed once exited.
- Scope is limited to the method, constructor, or block.
Instance Variables
- Declared inside a class but outside any method, constructor, or block.
- Created when an object is created and destroyed when the object is destroyed.
- Instance variables are unique to each instance of a class.
Static (Class) Variables
- Declared with the static keyword inside a class, outside any method, constructor, or block.
- Created when the program starts and destroyed when the program stops.
- Shared among all instances of a class.
Java Data Types
In Java, data types specify the size and type of values that can be stored in variables. Java supports two categories of data types:
1. Primitive Data Types
Introduction
- Java supports eight built-in data types known as primitive data types.
- Primitive data types are predefined by the language and named by a keyword.
Categories of Primitive Data Types
- Integer Types
- Floating-Point Types
- Character Type
- Boolean Type
Integer Types
byte
- Size: 8-bit
- Range: -128 to 127
short
- Size: 16-bit
- Range: -32,768 to 32,767
int
- Size: 32-bit
- Range: -2^31 to 2^31 – 1
long
- Size: 64-bit
- Range: -2^63 to 2^63 – 1
Floating-Point Types
- float
- Size: 32-bit
- Single-precision
- double
- Size: 64-bit
- Double-precision
boolean
Size: Not precisely defined (depends on JVM)
- Values: true or false
Character Type
- char
- Size: 16-bit
- Represents a single 16-bit Unicode character
- Range: ‘\u0000’ (0) to ‘\uffff’ (65,535)
Default Values
- byte: 0
- short: 0
- int: 0
- long: 0L
- float: 0.0f
- double: 0.0d
- char: ‘\u0000’
- boolean: false
2. Non-Primitive Data Types
Introduction
- Non-primitive data types, also known as reference types, refer to objects.
- Unlike primitive types, non-primitive types are created by the programmer.
Categories of Non Primitive Data Types
- Strings
- Arrays
- Classes
- Interfaces
- Collections
Strings
- A sequence of characters.
- Defined using the String class.
- Immutable: once created, their values cannot be changed.
public class StringExample
{
public static void main(String[] args)
{
String str = "Hello, World!";
System.out.println(str);
}
}
Arrays
- A collection of elements of the same type.
- Fixed in size once created.
- Can be one-dimensional or multi-dimensional.
public class ArrayExample
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5}; for (int num : arr)
{
System.out.println(num);
}
}
}
Classes
- Blueprint for creating objects.
- Contains fields (variables) and methods to define behaviors.
class Dog
{
String breed;
int age;
void bark()
{
System.out.println("Woof!");
}
}
public class ClassExample
{
public static void main(String[] args)
{
Dog myDog = new Dog();
myDog.breed = "Golden Retriever";
myDog.age = 5;
myDog.bark();
System.out.println("Breed: " + myDog.breed + ", Age: " + myDog.age);
}
}
Interview Questions
Q-1:- What is type casting? Explain with an example.
A-1:- Type casting is converting a variable from one data type to another. There are two
types of casting in Java:
Q-2:- What is the difference between float and double?
A-2:- float: Single precision (32 bits), approximately 6-7 decimal digits.
double: Double precision (64 bits), approximately 15-16 decimal digits.
Q-3:- What are the major differences in data types between Java and JavaScript?
A-3:- Java has strong and static typing. Data types must be declared explicitly, and type
checking is done at compile-time. Examples include int, double, char, boolean,
String.
JavaScript has dynamic typing. Variables can hold any type of data, and types are
checked at runtime. Examples include number, string, boolean, object,
undefined, null.
JAVA MCQ
Q-1:- What is the default value of a local variable in Java?
A) 0
B) null
C) false
D) No default value
Q-2:- Which of the following is not a primitive data type in Java?
A) int
B) float
C) String
D) boolean
Q-3:- Which of the following is not a keyword in Java?
A) static
B) Boolean
C) void
D) private