0% found this document useful (0 votes)
7 views5 pages

Java Programming Basics: Variables & Operators

This document provides an introduction to Java programming, covering the basics of writing, compiling, and executing simple Java applications. It explains key concepts such as variables, primitive data types, arrays, and various operators used in Java. The document includes examples and syntax for better understanding of these fundamental programming elements.

Uploaded by

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

Java Programming Basics: Variables & Operators

This document provides an introduction to Java programming, covering the basics of writing, compiling, and executing simple Java applications. It explains key concepts such as variables, primitive data types, arrays, and various operators used in Java. The document includes examples and syntax for better understanding of these fundamental programming elements.

Uploaded by

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

Topics: Getting started with java program: simple java applications, compiling a program,

and executing applications, Variables, Primitive Data Types, Arrays, Operators:


Assignment, Relational, Unary, Arithmetic, Conditional and others

1. Getting Started with Java Program


Simple Java Application

A Java program is written using classes and executed through the main() method.

Example: Simple Java Program


class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}

Explanation

●​ class HelloWorld → Defines a class​

●​ public static void main(String[] args) → Entry point of Java program​

●​ [Link]() → Prints output to the console​

2. Compiling and Executing a Java Program


Steps to Compile and Run Java Program

1.​ Write the program


○​ Save the file as: [Link]

2. Compile the program​



javac [Link]

●​ Generates bytecode file: [Link]​


3. Execute the program​

java HelloWorld

●​ JVM executes the bytecode​

3. Variables in Java
Definition

A variable is a container that stores data during program execution.

Syntax
dataType variableName = value;

Example
int age = 20;
float marks = 85.5f;
char grade = 'A';

Types of Variables

●​ Local variables​

●​ Instance variables​

●​ Static variables​

4. Primitive Data Types in Java


Java has 8 primitive data types:

Data Type Size Example

byte 1 byte byte b = 10;


short 2 short s = 100;
bytes

int 4 int i = 500;


bytes

long 8 long l = 10000L;


bytes

float 4 float f = 5.5f;


bytes

double 8 double d = 99.99;


bytes

char 2 char c = 'A';


bytes

boolean 1 bit boolean flag = true;

5. Arrays in Java
Definition

An array stores multiple values of the same data type.

Declaration and Initialization


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

Accessing Elements
[Link](numbers[0]); // Output: 10

Using Loop
for (int i = 0; i < [Link]; i++) {
[Link](numbers[i]);
}

6. Operators in Java
Operators are used to perform operations on variables.
(a) Assignment Operators

Used to assign values.

Operator Example

= a=5

+= a += 2

-= a -= 2

*= a *= 2

/= a /= 2

(b) Relational Operators

Used for comparison.

Operator Meaning

== Equal to

!= Not equal

> Greater than

< Less than

>= Greater than or


equal

<= Less than or equal

(c) Unary Operators

Operate on a single operand.

Operator Description

+ Unary plus

- Unary minus

++ Increment
-- Decrement

! Logical NOT

(d) Arithmetic Operators

Used for mathematical operations.

Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

(e) Conditional Operator (Ternary)


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

(f) Other Operators

●​ Logical Operators: &&, ||, !​

●​ Bitwise Operators: &, |, ^, ~, <<, >>​

●​ Instanceof Operator​
if (obj instanceof String)

You might also like