Unit 1 of 4 · M.Sc IT Sem 4

Unit 1: Java fundamentals and control structures

Java Technologies notes · PTU syllabus (PGCA1939)

3 min read9 topics9 exam questions
On this page
  1. Unit summary
  2. Origin and features of Java
  3. The Java environment and API
  4. Structure of a simple Java program
  5. Variables, literals and primitive data types
  6. The String class
  7. Operators and expressions
  8. Decision-making statements
  9. Looping statements
  10. Jump statements
  11. Key terms
  12. Quick revision
  13. Important questions

Unit summary

Java is a platform-independent, object-oriented language used for enterprise, web and Android software. This unit covers its origin and features, the Java API, variables, literals, primitive types, the String class, operators, decision-making, loops and jump statements.

After this unit you can

  • Explain the origin, features and environment of Java
  • Use variables, literals, primitive types and Strings
  • Apply operators and expressions
  • Write decision, loop and jump statements

PTU syllabus topics

  • Introduction to Java and its origin/features
  • Java API
  • variables
  • literals
  • primitive data types
  • the String class
  • operators
  • decision-making and looping statements
  • jump statements
ComparisonJava primitive data types
Size
Example

byte / short

8 / 16 bits

byte b = 10;

int / long

32 / 64 bits

long n = 9000L;

float / double

32 / 64 bits

double d = 3.14;

char

16 bits (Unicode)

char c = 'A';

boolean

true or false

boolean ok = true;

1

Topic 1

Origin and features of Java

ProcessHistory of Java
  1. 1

    1991

    James Gosling's team at Sun Microsystems starts the Green project; language named Oak

  2. 2

    1995

    Renamed Java; released with "write once, run anywhere"

  3. 3

    2006–07

    Open-sourced as OpenJDK

  4. 4

    2010

    Oracle acquires Sun

  5. 5

    2014

    Java 8: lambdas and streams

  6. 6

    2018 onward

    Six-monthly releases; long-term support versions 11, 17, 21 and 25

Key termsFeatures of Java
Simple
No pointers or operator overloading
Object-oriented
Everything inside classes
Platform independent
Compiled to bytecode that runs on any JVM
Secure
No explicit pointers, bytecode verifier, sandbox
Robust
Strong typing, exception handling, garbage collection
Multithreaded
Built-in threads
Distributed and dynamic
Networking libraries; classes loaded at run time
High performance
Just-in-time (JIT) compilation
2

Topic 2

The Java environment and API

ProcessCompile and run
  1. 1Write Hello.java
  2. 2javac Hello.java

    Compiler produces Hello.class (bytecode)

  3. 3java Hello

    JVM loads, verifies and runs bytecode

  4. 4JIT compiles hot code to machine code
Key termsJDK, JRE and JVM
JVM
Java Virtual Machine — executes bytecode; platform specific
JRE
JVM plus core libraries — needed to run programs
JDK
JRE plus development tools — javac, java, javadoc, jar, jdb
  • API: thousands of ready classes organised in packages — java.lang, java.util, java.io, java.awt, java.net.
3

Topic 3

Structure of a simple Java program

java// Documentation section: program to greet a student
package college;                 // optional package statement
import java.util.Scanner;        // import statements

public class Hello {             // class definition; file must be Hello.java
    public static void main(String[] args) {   // entry point
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = sc.nextLine();
        System.out.println("Welcome to SBS, " + name);
    }
}
  • main: public (accessible to the JVM), static (called without an object), void (returns nothing), String[] args (command-line arguments).
4

Topic 4

Variables, literals and primitive data types

ClassificationJava data types
Data types
  • Primitive integer

    byte (1 byte), short (2), int (4), long (8)

  • Primitive floating

    float (4), double (8)

  • Primitive others

    char (2 bytes, Unicode), boolean (true or false)

  • Reference

    Classes, interfaces, arrays, String

javafinal double PI = 3.14159;       // constant
int age = 19;                    // variable
long population = 1_400_000_000L;
float cgpa = 8.6f;
char grade = 'A';
boolean passed = true;
double avg = (double) 245 / 3;   // explicit casting
int x = 'A' + 1;                 // implicit widening: 66
var city = "Ludhiana";           // local type inference (Java 10+)
  • Variable kinds: local (inside methods), instance (per object), class or static (shared by all objects). Type conversion: widening is automatic (int → long → float → double); narrowing needs a cast.
5

Topic 5

The String class

javaString s = "SBS";
String t = s + " Bathinda";           // concatenation creates a new object
System.out.println(t.length());        // 12
System.out.println(t.toUpperCase());   // SBS BATHINDA
System.out.println(s.equals("sbs"));   // false (use equalsIgnoreCase)
System.out.println(t.substring(4));    // Bathinda
  • Strings are immutable; literals are pooled. Use equals() not == to compare content.
6

Topic 6

Operators and expressions

Key termsJava operators
Arithmetic
+ − × / % (multiplication is written with the asterisk symbol)
Relational
< <= > >= == !=
Logical
&&, the double-bar OR operator, !
Bitwise and shift
&, ^, ~, <<, >>, >>>
Assignment
= += −= and so on
Increment and decrement
++ and −−
Conditional
condition ? value1 : value2
instanceof
Checks an object's type
  • Precedence: unary > multiplicative > additive > shift > relational > equality > bitwise > logical > conditional > assignment. Integer division truncates (7 / 2 = 3); % gives the remainder.
7

Topic 7

Decision-making statements

javaint marks = 72;
if (marks >= 75) System.out.println("Distinction");
else if (marks >= 60) System.out.println("First division");
else System.out.println("Pass");

int day = 3;
switch (day) {
    case 1: System.out.println("Monday"); break;
    case 3: System.out.println("Wednesday"); break;
    default: System.out.println("Other day");
}
String type = switch (day) {               // switch expression (Java 14+)
    case 6, 7 -> "Weekend";
    default -> "Weekday";
};
8

Topic 8

Looping statements

javafor (int i = 1; i <= 10; i++) System.out.print(i * i + " ");   // squares

int n = 12345, sum = 0;
while (n > 0) { sum += n % 10; n /= 10; }                      // digit sum = 15

int k = 1;
do { System.out.println(k); k++; } while (k <= 3);             // runs at least once

int[] marks = {78, 85, 90};
for (int m : marks) System.out.println(m);                     // enhanced for

outer:
for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) {
        if (i * j == 4) break outer;                            // labelled break
        if (j == 2) continue;
        System.out.println(i + "," + j);
    }
9

Topic 9

Jump statements

Key termsJump statements
break
Exit the nearest loop or switch; labelled break exits an outer loop
continue
Skip to the next iteration
return
Exit a method, optionally with a value
javaouter:
for (int i = 1; i <= 3; i++)
    for (int j = 1; j <= 3; j++) {
        if (j == 2) continue;
        if (i == 3) break outer;
        System.out.println(i + "," + j);   // 1,1 1,3 2,1 2,3
    }

Key terms

JVM
Java Virtual Machine running bytecode
Bytecode
Platform-independent compiled code
Literal
Fixed value written in code
Immutable
Cannot be changed after creation
Labelled break
break that exits a named outer loop

Quick revision

  • Origin (Sun, 1995); features; JDK, JRE, JVM, API.
  • Eight primitive types; literals; Strings.
  • Operators; if, switch, loops; break, continue, return.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Why is Java platform independent?
  2. Q2.List the primitive data types.
  3. Q3.Why are Strings immutable?
  4. Q4.Distinguish == and equals().
  5. Q5.What is a labelled break?
  6. Q6.Distinguish while and do-while.

Long-answer questions

  1. Q1.Explain the features of Java.
  2. Q2.Explain data types, variables and literals in Java.
  3. Q3.Explain control and jump statements with examples.

Stuck on this unit?

Message SBS on WhatsApp for help with Java Technologies, or to ask about studying M.Sc IT at Synetic.

WhatsApp us