[Go to site: main page, start]

0% found this document useful (0 votes)
3 views22 pages

Introduction To Java

Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995, widely used for various applications including desktop, web, and mobile. Key features include platform independence, robust security, multithreading, and a rich set of libraries and frameworks. The document also covers Java syntax, data types, variables, operators, control statements, and loops, providing examples and explanations for each concept.

Uploaded by

safikafathima951
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Introduction To Java

Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995, widely used for various applications including desktop, web, and mobile. Key features include platform independence, robust security, multithreading, and a rich set of libraries and frameworks. The document also covers Java syntax, data types, variables, operators, control statements, and loops, providing examples and explanations for each concept.

Uploaded by

safikafathima951
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to Java

Java is a high-level, object-oriented programming language developed by Sun

Microsystems in 1995. It is mostly used for building desktop applications, web

applications, Android apps and enterprise systems.

Features of Java

 Object-Oriented Programming (OOP): Java supports OOP concepts to create

modular and reusable code.

 Platform Independence: Java programs can run on any operating system with a

JVM.

 Robust and Secure: Java ensures reliability and security through strong

memory management and exception handling.

 Multithreading and Concurrency: Java allows concurrent execution of multiple

tasks for efficiency.

 Rich API and Standard Libraries: Java provides extensive built-in libraries for

various programming needs.

 Frameworks for Enterprise and Web Development: Java supports frameworks

that simplify enterprise and web application development.

 Open-Source Libraries: Java has a wide range of libraries to extend

functionality and speed up development.

 Maintainability and Scalability: Java’s structured design allows easy

maintenance and growth of applications.


Understanding the Hello World Program in Java

When we learn any programming language, the first step is writing a simple

program to display "Hello World". So, here is a simple Java program that

displays "Hello World" on the screen.

public class HelloWorld {

public static void main(String[] args) {

[Link]("Hello World!");

Output

Hello World!

Explanation:

Hello World Program Explanation

How to run the above code?

 Write code in a file like [Link].

 Java Compiler "javac" compiles it into bytecode "[Link]".

 JVM (Java Virtual Machine) reads the .class file and interprets the bytecode.

 JVM converts bytecode to machine readable code i.e. "binary" (001001010)

and then execute the program.


Java Program Execution Flow

Comments in Java

Comments are the notes written inside the code to explain what we are doing.

The comment lines are not executed while we run the program.

Single-line comment

// This is a comment

Multi-line comment

/*

This is a multi-line comment.

This is useful for explaining larger sections of code.

*/

Curly Braces and Indentation in Java

In Java, curly braces {} are used to define blocks of code. For example, the body

of a class or method is enclosed within curly braces.

Example:

public class Geeks{

public static void main(String[] args) {

[Link]("This is inside the block.");


}

[Link]("This is outside the block.");

 The code block inside {} runs unconditionally and prints a message.

 Curly braces {} group multiple statements into a single block, even without

conditions.

Famous Applications Built Using Java

 Android Apps: Most of the Android mobile apps are built using Java.

 Netflix: This uses Java for content delivery and backend services.

 Amazon: Java language is used for its backend systems.

 LinkedIn: This uses Java for handling high traffic and scalability.

 Minecraft: This is one of the world’s most popular games that is built in Java.

 Spotify: This uses Java in parts of its server-side infrastructure.

 Uber: Java is used for backend services like trip management.

 NASA WorldWind: This is a virtual globe software built using Java.

What Can We Do with Java?

Java is used for:

 Mobile App Development: Android development using Android Studio.

 Web Development: Using frameworks like Spring, Spring

Boot, Struts, Hibernate


 Desktop GUI Applications: With libraries like JavaFX and Swing.

 Enterprise Applications: Backbone of banking, ERP and large-scale business

software.

 Game Development: With engines like LibGDX and jMonkeyEngine.

 Big Data Technologies: Like Hadoop and Apache Kafka.

 Internet of Things (IoT): Java can run on embedded systems and devices.

 Cloud-based Applications : Java is used in services on AWS, Azure and Google

Cloud.

 Scientific Applications: Java is used in tools that process large amounts of

scientific data.

Java is a class-based, object-oriented programming language that is designed to

be secure and portable. Its core principle is “Write Once, Run Anywhere”

(WORA), meaning Java code can run on any device or operating system that has

a Java Virtual Machine (JVM).

Java Development Environment:

To run Java on your machine, you first need to set up the Java environment

properly. This includes configuring the required environment variables (such as

PATH and JAVA_HOME).

You can refer to Setting up Environment Variables For Java article for a step-by-

step guide on setting up Java environment variables.

The development environment of Java consists of three components mainly:


1. JVM (Java Virtual Machine): JVM is the engine that runs Java programs. It

converts Java bytecode (compiled code) into machine code (understandable by

the OS).

2. JRE (Java Runtime Environment): JRE = JVM + Libraries + Other

components needed to run Java applications.

3. JDK (Java Development Kit): JDK = JRE + Development Tools. It is

required for developing Java applications.

Java Basic Syntax:

Before writing complex programs, it is important to understand the basic syntax

of Java. The syntax defines the rules and structure of how Java code is written

and executed. Every Java program is built using classes, methods, and statements.

Example:

public class HelloWorld {

public static void main(String[] args)

[Link]("Hello, World");

Output
Hello, World

Explanation:

 public class HelloWorld: Defines a class named HelloWorld.

 public static void main(String[] args): The entry point of the program;

execution starts here.

 [Link]("Hello, World"): Prints the text "Hello, World"

followed by a new line.

To learn Java syntax in depth and understand how this Hello World program

runs, you may refer to this article: Java Hello World Program

Comments in Java:

Comments in Java are notes written inside the code that are ignored by the

compiler. They are used to make the code more understandable and maintainable.

Example:

public class CommentsExample {

public static void main(String[] args) {

// This is a single-line comment

[Link]("Hello, World"); // Comment at end of line

/*
* This is a multi-line comment

* It can span across multiple lines

*/

int x = 10; // Variable declaration

/**

* This is a documentation comment (Javadoc)

* It is used to generate documentation for methods, classes, etc.

*/

[Link]("Value of x: " + x);

To know more about comments and it's types, you may refer to this article: Java

Comments

Data Types in Java:

In Java, data types specify the type of values a variable can hold. They define the

size, range and nature of data stored in memory. Java has two main categories of

data types:

1. Primitive: byte, short, int, long, float, double, char, boolean

2. Non-Primitive: String, Arrays, Classes, Interfaces, Objects


public class DataTypesDemo {

public static void main(String[] args) {

// -------- Primitive Data Types --------

byte b = 100; // 1 byte

short s = 30000; // 2 bytes

int i = 100000; // 4 bytes

long l = 10000000000L; // 8 bytes

float f = 3.14f; // 4 bytes

double d = 3.14159265359; // 8 bytes

char c = 'A'; // 2 bytes (Unicode character)

boolean flag = true; // 1 bit

// -------- Non-Primitive Data Types --------

String str = "Hello, Java"; // String (class in Java)

int[] arr = {1, 2, 3, 4, 5}; // Array

Integer wrapperInt = [Link](50); // Wrapper class example

StringBuilder sb = new StringBuilder("Java"); // Class object

// -------- Output --------

[Link]("byte: " + b);

[Link]("short: " + s);

[Link]("int: " + i);


[Link]("long: " + l);

[Link]("float: " + f);

[Link]("double: " + d);

[Link]("char: " + c);

[Link]("boolean: " + flag);

[Link]("String: " + str);

[Link]("Array: ");

for (int num : arr) {

[Link](num + " ");

[Link]();

[Link]("Wrapper Integer: " + wrapperInt);

[Link]("StringBuilder: " + sb);

Output

byte: 100

short: 30000

int: 100000

long: 10000000000

float: 3.14
double: 3.14159265359

char: A

boolean: true

String: Hello, Java

Array: 1 2 3 4 5

Wrapper Integer: 50

StringBuilder: Java

To know Data Types and it's categories in more detail, you may refer to this

article: Java Data Types

Variables in Java:

Variables are containers to store data in memory. Each variable has a name, type

and value. It is the basic unit of storage in a program. Java has 4 types of

variables.

 Local Variables: Declared inside a method, constructor, or block. Accessible

only within that block.

 Instance Variables: Declared inside a class but outside any method. Each

object of the class has its own copy.

 Static Variables: Declared with the static keyword inside a class. Shared by

all objects of the class.


 Final Variables: Declared with final keyword. Value cannot be changed once

assigned.

public class VariablesDemo {

// Instance variable (belongs to each object)

int instanceVar = 10;

// Static variable (shared across all objects of the class)

static String staticVar = "I am static";

public void showVariables() {

// Local variable (declared inside a method)

int localVar = 5;

[Link]("Instance Variable: " + instanceVar);

[Link]("Static Variable: " + staticVar);

[Link]("Local Variable: " + localVar);

public static void main(String[] args) {

// Creating object

VariablesDemo obj1 = new VariablesDemo();


[Link]();

// Accessing static variable directly using class name

[Link]("Accessing Static Variable via class: " +

[Link]);

Output

Instance Variable: 10

Static Variable: I am static

Local Variable: 5

Accessing Static Variable via class: I am static

Explanation:

 Local Variable: Declared inside methods, constructors, or blocks.

 Instance Variable: Declared inside the class but outside methods, each object

gets its own copy.

 Static Variable: Declared with static, shared among all objects of the class.

To know more about variables, you may refer to this article: Java Variables

Keywords in Java:
Keywords are reserved words in Java that have a predefined meaning. They

cannot be used as variable names, class names or identifiers.

Examples: new, package, private, protected, public, return, short, static, etc.

To know all keywords in Java, you may refer to this article: Java Keywords

Operators in Java:

Operators are symbols that perform specific operations on one or more operands

(variables or values). They are used to perform calculations, comparisons, logical

operations and manipulate data.

They are basically of 7 types:

 Arithmetic Operators: +, -, *, /, %

 Relational Operators: ==, !=, >, <, >=, <=

 Logical Operators: &&, ||, !

 Assignment Operators: =, +=, -=, *=, /=, %=

 Unary Operators: +, -, ++, --, !

 Ternary Operator: condition ? value_if_true : value_if_false

 Bitwise Operators: &, |, ^, ~, <<, >>, >>>

public class SimpleOperatorsDemo {

public static void main(String[] args) {


int a = 10, b = 3;

// Arithmetic Operators

[Link]("a + b = " + (a + b)); // Addition

[Link]("a - b = " + (a - b)); // Subtraction

// Relational Operator

[Link]("a > b ? " + (a > b)); // Greater than

// Logical Operator

boolean x = true, y = false;

[Link]("x && y = " + (x && y)); // Logical AND

// Assignment Operator

a += 5; // a = a + 5

[Link]("a after += 5 : " + a);

// Ternary Operator

int max = (a > b) ? a : b;

[Link]("Maximum = " + max);

Output

a + b = 13
a-b=7

a > b ? true

x && y = false

a after += 5 : 15

Maximum = 15

To learn about Operators in Java, you may refer to this article: Java Operators

Decision Making (Control Statements) in Java:

Decision-making (or control statements) are used to execute different blocks of

code based on certain conditions. They allow a Java program to choose a path of

execution depending on whether a condition is true or false.

Examples:

 if: Executes a block if a condition is true.

 if-else: Chooses between two blocks based on a condition.

 if-else if-else: Tests multiple conditions sequentially.

 switch: Selects one block from multiple options based on a variable’s value.

public class DecisionMakingDemo {

public static void main(String[] args) {

int number = 10;


// if statement

if (number > 0) {

[Link]("The number is positive.");

// if-else statement

if (number % 2 == 0) {

[Link]("The number is even.");

} else {

[Link]("The number is odd.");

// if-else-if ladder

if (number < 0) {

[Link]("The number is negative.");

} else if (number == 0) {

[Link]("The number is zero.");

} else {

[Link]("The number is positive.");

// switch statement

int day = 3;

switch (day) {
case 1:

[Link]("Monday");

break;

case 2:

[Link]("Tuesday");

break;

case 3:

[Link]("Wednesday");

break;

default:

[Link]("Other day");

Output

The number is positive.

The number is even.

The number is positive.

Wednesday

Explanation:

 if: checks if a number is positive.

 if-else: checks if the number is even or odd.

 if-else-if: checks whether the number is negative, zero, or positive.


 switch-case: prints the day of the week based on the value of day.

Loops in Java:

Loops are control statements in Java that allow a block of code to be executed

repeatedly as long as a specified condition is true. They help in reducing code

repetition.

There are 4 types of loops in Java.

 for: Used when the number of iterations is known.

 while: Used when the number of iterations is not known in advance, condition

checked before each iteration.

 do-while: Similar to while loop, but condition is checked after executing the

block (executes at least once).

 for-each: Used to iterate over arrays and collections.

public class LoopsDemo {

public static void main(String[] args) {

// 1. For loop

[Link]("For Loop:");

for (int i = 1; i <= 5; i++) {

[Link]("i = " + i);


}

// 2. While loop

[Link]("\nWhile Loop:");

int j = 1;

while (j <= 5) {

[Link]("j = " + j);

j++;

// 3. Do-While loop

[Link]("\nDo-While Loop:");

int k = 1;

do {

[Link]("k = " + k);

k++;

} while (k <= 5);

// 4. Enhanced For Loop (for-each loop)

[Link]("\nEnhanced For Loop:");

int[] numbers = {10, 20, 30, 40, 50};

for (int num : numbers) {

[Link]("num = " + num);

}
}

Output

For Loop:

i=1

i=2

i=3

i=4

i=5

While Loop:

j=1

j=2

j=3

j=4

j=5

Do-While Loop:

k=1

k=2
k=3

k=4

k=5

Enhanced For Loop:

num = 10

num = 20

num = 30

num = 40

You might also like