[Go to site: main page, start]

0% found this document useful (0 votes)
6 views12 pages

Java Module 1 2

The document provides an overview of Java programming, including its history, structure, and key components such as variables, data types, and constants. It outlines the basic structure of a Java program, the memory map in the Java Virtual Machine (JVM), and details about Java tokens, keywords, and data types. Additionally, it explains the concept of constants and enumerations in Java, highlighting their usage and significance.

Uploaded by

nagadhanush700
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)
6 views12 pages

Java Module 1 2

The document provides an overview of Java programming, including its history, structure, and key components such as variables, data types, and constants. It outlines the basic structure of a Java program, the memory map in the Java Virtual Machine (JVM), and details about Java tokens, keywords, and data types. Additionally, it explains the concept of constants and enumerations in Java, highlighting their usage and significance.

Uploaded by

nagadhanush700
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

STRUCTURE OF JAVA PROGRAM

VARIABLES
RESERVED WORDS
DATA TYPES
CONSTANTS

COURSE INSTRUCTOR
Java Fundamentals | Module 1.2
HISTORY OF JAVA

❑ Developed by James Gosling at Sun Microsystems in 1995.

❑ Originally called 'Oak', later renamed Java.

❑ Designed with the principle: Write Once, Run Anywhere (WORA).

❑ Java is widely used for web apps, Android, enterprise software, and embedded systems.
STRUCTURE OF JAVA PROGRAM
// Comments (optional) – describe the program
package mypackage; // optional – organise classes
import [Link]; // optional – include library classes

public class HelloWorld { // Class declaration (mandatory)


// Field declarations (optional) – class-level variables
static int globalVar = 10;

public static void main(String[] args) { // Entry point (mandatory)


// Local declarations
int localVar = 5;
// Executable statements
[Link]("Hello, World!");
return; // optional in void methods
}

// User-defined methods (optional)


static void myMethod() { /* definition */ }
}
SAMPLE JAVA PROGRAM
/* A simple Java program */
import [Link].*; // Standard I/O library /* … */
Multi-line comment
public class Sample {
public static void main(String[] args) {
import
[Link]("Java is Great"); // print with newline
Includes external packages (like #include in C)
return;
}
} public class
All code lives inside a class

main(String[] args)
Entry point – args receives command-line input

[Link]()
Standard output (like printf in C)

return;
Exits the method (optional in void)
MEMORY MAP OF JAVA PROGRAM (JVM)

Method Area Stores class metadata, static variables, bytecode


High Address

Heap Objects & instance variables; Garbage Collected

Stack Local variables & method call frames (per thread)

PC Register Holds address of current bytecode instruction

Native Stack Used for native (non-Java) method calls

Low Address
JAVA TOKENS
Keywords
Tokens are the smallest meaningful units that theIdentifiers
Java compiler recognises. Literals
class, int, while, if, for, … main, amount, myVar, … -50, 3.14, 'A', true, "Hello"

Operators Separators Comments


+ – * / % == && … {} () [] ; , . // /* */ /** */
VARIABLES
In Java, a variable is a named container (storage area) that holds data.
Each variable has a type, a name (identifier), and a value.
Variable names are symbolic representations of memory locations.

4 2.5 'a' "hello"


int myInt = 4; double myReal = 2.5; char myChar = 'a'; String myStr = "hello";

Rules for naming a variable (identifier):


1. Can contain letters, digits, $ and _ (no spaces or special chars).
2. Must NOT start with a digit.
3. Case-sensitive: myVar ≠ MyVar.
4. Cannot be a Java keyword.
5. No length limit in practice (but keep it readable).

Local vs Instance vs Static (Class) Variables:


Local – declared inside a method, no default value. | Instance – per object, default 0/false/null. | Static – shared across all objects.
KEYWORDS (RESERVED WORDS)
Keywords are reserved words with predefined meaning, already known to the Java compiler.

abstract assert boolean break byte case catch char class const

continue default do double else enum extends final finally float

for goto if implements import instanceof int interface long native

new package private protected public return short static strictfp super

switch synchronized this throw throws transient try void volatile while

Data Types (8) Flow Control (11) OOP Keywords (10)


boolean byte char double float int long short if else for while do switch case break continue default class interface extends implements new this super abstr

Access Modifiers (3) Exception (5)


public private protected try catch finally throw throws
DATA TYPES
Java data types tell the compiler: (1) how to interpret stored data, (2) how many bytes to allocate.
Data Type

Primitive Reference /
Non-Primitive

byte short int long String Arrays Classes Interfaces

float double char boolean

Java type modifiers:


Access: public / private / protected | Non-access: final, static, abstract, volatile, transient, synchronized
DATA TYPES – PRIMITIVE TYPES

Type Size (bits) Default Min Range Max Range Example

byte 8 0 -128 127 byte b = 10;

short 16 0 -32,768 32,767 short s = 100;

int 32 0 -2,147,483,648 2,147,483,647 int i = 5;

long 64 0L -9.2×10¹■ 9.2×10¹■ long l = 100L;

float 32 0.0f 1.4E-45 3.4E+38 (6-7 digits) float f = 3.14f;

double 64 0.0d 4.9E-324 1.8E+308 (15-16 digits) double d = 3.14;

char 16 \u0000 0 (Unicode) 65,535 char c = 'A';

boolean 1 false – – boolean flag = true;

Notes:
• String is NOT a primitive – it is a class (reference type). • Use wrapper classes (Integer, Double …) to use primitives as objects. • Java has NO unsigned integer types.
CONSTANTS

Constants

Numeric Character
Constants Constants

Integer Floating-point Single char String


123, -321, 0xFF 0.09, 3.14f, 2.5d 'a', '5', 'Z' "Hello", "Java"

Constant Declaration
// 1. Using in Java:
'final' keyword
final double PI = 3.14159;
// PI = 3.0; // ERROR – cannot reassign a final variable

// 2. Static final (class constant – equivalent to C's #define)


public static final int MAX_SIZE = 100;

// 3. Enum constants
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
Day today = [Link]; // today == 2 (ordinal)
ENUMERATION CONSTANTS
An enum is a special class that represents a group of named constants.

Enum values are objects in Java (unlike C where they are plain integers).

Values have an ordinal (0-based index) and a name() method.


enum Week { MON, TUE, WED, THU, FRI, SAT, SUN }
Output:
public class EnumDemo {
public static void main(String[] args) { WED
Week day = [Link];
[Link](day); // WED 2
[Link]([Link]()); // 2
[Link]([Link]()); // WED WED
}
}

Custom Enum Output:


enum Day {
SUN(1), MON(2), TUE(20), WED(21), 1 2 20 21 10 11 12
THU(10), FRI(11), SAT(12);
private final int val;
Day(int v) { [Link] = v; }
public int getVal() { return val; }
}
// [Link]() → 1
// [Link]() → 20

You might also like