JAVA PROGRAMMING
What is java ?
Java is a high-level, object-oriented programming language designed for building cross-platform applications, known for its simplicity, security, and platform independence through the use of the Java Virtual Machine (JVM).java
Java was developed by Sun Microsystems. The development started in 1991, and the language was officially released in 1995.VIDEO LINK
![]() |
JAVA |
It used for:
- Mobile applications (specially Android apps)
- Desktop applications
- Web applications
- Web servers and application servers
- Games
- Database connection
- And much, much more!
Why Use Of Java ?
Platform Independence: Java code can run on any device with a Java Virtual Machine (JVM), making it highly portable.
Object-Oriented: It promotes modular, reusable code through object-oriented principles.
Security: Java has built-in security features, making it safer for network-based applications.
Robustness: With strong memory management, exception handling, and type-checking, Java is less prone to errors.
Scalability: Java is suitable for developing both small and large-scale applications.
Multi-threading: Java supports concurrent execution, enabling better performance for complex applications.
Large Community and Support: A vast developer community, extensive libraries, and frameworks make Java a reliable choice.
Java Syntax :
Java syntax refers to the rules that define how Java programs are written and interpreted. Here's a basic structure of Java syntax:
1. Class Definition: Every Java program must define at least one class.
class MyClass {
// Class body
}
2. Main Method: The entry point of every Java application. It is where the program starts executing.
public class MyClass {
public static void main(String[] args) {
// Code to be executed
}
}
3. Statements: Each line of code inside methods ends with a semicolon (`;`).
System.out.println("Hello, World!");
```
4. Variables: Declaring and initializing variables follows a type-first syntax.
int number = 10;
String name = "Java";
5. Control Structures: Like `if`, `for`, `while` statements.
if (number > 0) {
System.out.println("Positive number");
}
6. Methods: Defined to encapsulate behavior.
public static void greet() {
System.out.println("Hello!");
}
This is a basic example of Java syntax:
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
In this example:
- `public class MyClass` defines the class.
- `public static void main(String[] args)` defines the main method.
- `System.out.println` prints text to the console.
Java Data types :
In Java, data types specify the type of data that can be stored in variables. They are categorized into two main types:
1. Primitive Data Types: These are the basic data types in Java.
byte: 1 byte (8 bits), stores integer values from -128 to 127.
byte myByte = 100;
short : 2 bytes (16 bits), stores integer values from -32,768 to 32,767.
short myShort = 5000;
int: 4 bytes (32 bits), stores integer values from -2^31 to 2^31-1.
int myInt = 100000;
long: 8 bytes (64 bits), stores large integer values from -2^63 to 2^63-1.
long myLong = 15000000000L;
float: 4 bytes (32 bits), stores decimal values with single precision.
float myFloat = 5.75f;
double: 8 bytes (64 bits), stores decimal values with double precision.
double myDouble = 19.99;
char: 2 bytes (16 bits), stores a single character or Unicode value.
char myChar = 'A';
boolean: 1 bit, stores `true` or `false`.
boolean isJavaFun = true;
2. Non-Primitive (Reference) Data Types: These include objects, arrays, and strings. They store memory addresses of objects rather than the data itself.
String: Used to store a sequence of characters.
String myString = "Hello, Java!";
Arrays: Used to store multiple values of the same type.
int[] myArray = {1, 2, 3, 4, 5};
Classes: Used to create objects with custom data and methods.
MyClass obj = new MyClass();
Primitive types are predefined by the language, whereas non-primitive types are user-defined and created using classes.
Java Comments :
In Java, comments are used to make the code more readable and to provide explanations. They are ignored by the compiler. There are three types of comments in Java:
1. Single-line Comments: These comments start with `//` and are used for brief explanations.
// This is a single-line comment
System.out.println("Hello, Java!");
2. Multi-line Comments: These comments start with `/*` and end with `*/`. They can span multiple lines.
/* This is a multi-line comment
It can span across multiple lines */
System.out.println("Hello, Java!");
3. Documentation Comments: These comments start with `/**` and end with `*/`. They are used to generate documentation (using JavaDoc) and are placed before classes, methods, or fields.
/**
* This is a documentation comment
* It provides information about the class or method
*/
public class MyClass {
/**
* This method prints a greeting message
*/
public static void greet() {
System.out.println("Hello, Java!");
}
}
Java Operators :
In Java, operators are special symbols that perform operations on variables and values. They can be categorized as follows:
1. Arithmetic Operator
These operators are used to perform basic arithmetic operations:
`+` : Addition
`-` : Subtraction
`*` : Multiplication
`/` : Division
`%` : Modulus (remainder)
Example:
int a = 10;
int b = 5;
System.out.println(a + b); // Output: 15
2. Relational (Comparison) Operators
These operators compare two values:
`==` : Equal to
`!=` : Not equal to
`>` : Greater than
`<` : Less than
`>=` : Greater than or equal to
`<=` : Less than or equal to
Example:
int a = 10;
int b = 5;
System.out.println(a > b); // Output: true
3. Logical Operators
These operators are used to combine multiple boolean expressions:
`&&` : Logical AND
`||` : Logical OR
`!` : Logical NOT
Example:
boolean x = true;
boolean y = false;
System.out.println(x && y); // Output: false
4. Assignment Operators
These are used to assign values to variables:
`=` : Assign
`+=` : Add and assign
`-=` : Subtract and assign
`*=` : Multiply and assign
`/=` : Divide and assign
`%=` : Modulus and assign
Example:
int a = 10;
a += 5; // a = a + 5
System.out.println(a); // Output: 15
5. Unary Operators
These operate on a single operand:
`+` : Unary plus (indicates a positive value)
`-` : Unary minus (negates an expression)
`++` : Increment (increases value by 1)
`--` : Decrement (decreases value by 1)
Example:
int a = 10;
a++; // a becomes 11
System.out.println(a); // Output: 11
6. Bitwise Operators
These perform operations on bits:
`&` : Bitwise AND
`|` : Bitwise OR
`^` : Bitwise XOR
`~` : Bitwise NOT
`<<` : Left shift
`>>` : Right shift
`>>>` : Unsigned right shift
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(a & b); // Output: 1 (0001 in binary)
7. Ternary Operator
The ternary operator is a shorthand for the `if-else` statement:
- `? :`
Syntax:
condition ? expression1 : expression2;
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b; // Output: 20
These operators allow Java developers to perform calculations, logic, and control flow in their code.
0 Comments