Fundamental
Programming Structures in Java
Java provides a solid foundation for creating
reliable and maintainable software through various programming structures.
Let’s break down the fundamental elements:
1. Data Types and
Variables
Java is a statically-typed language, which means all
variables must be declared before use. The two main categories are:
a. Primitive Data
Types
- byte: 8-bit integer (-128 to 127)
- short: 16-bit integer (-32,768 to
32,767)
- int: 32-bit integer (-2^31 to
2^31 - 1)
- long: 64-bit integer (-2^63 to
2^63 - 1)
- float: 32-bit floating-point (Single-precision)
- double: 64-bit floating-point (Double-precision)
- char: 16-bit Unicode character ('\u0000' to '\uffff')
- boolean: True or False (true/false)
b. Reference Data
Types
- Objects: Instances of classes.
- Arrays: Objects that store multiple values of a single
type.
- Strings: Immutable objects representing sequences of
characters.
c. Variable
Declaration and Initialization
int age = 25;
double salary =
50000.0;
char grade = 'A';
boolean isJavaFun
= true;
2. Operators
Java supports various operators for manipulating
data. These include:
a. Arithmetic
Operators
+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus)
int result = 10 +
5; // Output: 15
b. Relational
Operators
==, !=, >, <, >=, <=
boolean isEqual =
(10 == 20); // Output: false
c. Logical
Operators
&& (AND), || (OR), ! (NOT)
boolean result =
(10 > 5) && (20 > 15); // Output: true
d. Assignment
Operators
=, +=, -=, *=, /=, %=
int x = 10;
x += 5; // x becomes 15
e.
Increment/Decrement Operators
++, --
int num = 5;
num++; // num becomes 6
3. Control Flow
Statements
Java provides mechanisms for making decisions and
controlling the execution flow.
a. Conditional
Statements
- if Statement
if (age >= 18)
{
System.out.println("You are an
adult.");
}
- if-else Statement
if (age >= 18)
{
System.out.println("You are an
adult.");
} else {
System.out.println("You are a
minor.");
}
- if-else-if Ladder
if (marks >=
90) {
System.out.println("Grade A");
} else if (marks
>= 80) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
- switch Statement
int day = 2;
switch (day) {
case 1:
System.out.println("Monday"); break;
case 2:
System.out.println("Tuesday"); break;
default: System.out.println("Invalid
Day");
}
b. Looping
Statements
- for Loop
for (int i = 0; i
< 5; i++) {
System.out.println(i);
}
- while Loop
int i = 0;
while (i < 5)
{
System.out.println(i);
i++;
}
- do-while Loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i <
5);
c. Enhanced for
Loop (for-each Loop)
Used for iterating over arrays or collections.
int[] numbers =
{10, 20, 30, 40};
for (int num :
numbers) {
System.out.println(num);
}
4. Arrays
Arrays are objects that store multiple values of the
same type.
a. Declaration
and Initialization
int[] numbers =
new int[5]; // Declaration
int[] scores =
{90, 80, 70, 60, 50}; // Initialization
b. Accessing
Elements
System.out.println(scores[0]); // Output: 90
c.
Multi-dimensional Arrays
int[][] matrix =
{ {1, 2, 3}, {4, 5, 6} };
System.out.println(matrix[1][2]); // Output: 6
5. Methods
(Functions)
Methods are reusable blocks of code that perform
specific tasks.
a. Declaration
and Definition
public static int
add(int a, int b) {
return a + b;
}
b. Calling a
Method
int sum = add(5,
10);
System.out.println(sum); // Output: 15
c. Method
Overloading
Creating multiple methods with the same name but
different parameters.
public int
add(int a, int b) { return a + b; }
public double
add(double a, double b) { return a + b; }
6. Classes and
Objects
Java is an object-oriented language, and everything
revolves around classes and objects.
a. Creating a
Class
public class
Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void display() {
System.out.println(name + " is
" + age + " years old.");
}
}
b. Creating an
Object
public class Main
{
public static void main(String[] args) {
Student student = new
Student("Krishna", 25);
student.display();
}
}
Output:
Krishna is 25
years old.
7. Input and
Output
Java provides several ways to take input and produce
output.
a. Standard
Output (Printing)
System.out.println("Hello,
World!");
b. Standard Input
(Using Scanner)
import
java.util.Scanner;
Scanner scanner =
new Scanner(System.in);
System.out.print("Enter
your name: ");
String name =
scanner.nextLine();
System.out.println("Hello,
" + name);
scanner.close();
8. Packages
Packages are collections of related classes and
interfaces that provide modularity.
Creating a
Package
package
mypackage;
public class
MyClass {
public void display() {
System.out.println("Inside
mypackage.");
}
}
Using a Package
import
mypackage.MyClass;
public class Main
{
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}
No comments:
Post a Comment