Docsity

Core Java: Introduction to Programming Concepts and Object-Oriented Programming - Prof. Tu, Lecture notes of Java Programming

Partial preview of the text

Download Core Java: Introduction to Programming Concepts and Object-Oriented Programming - Prof. Tu and more Lecture notes Java Programming in PDF only on Docsity!

Core Java

  • Sharad Ballepu

Servlets & JSPs

Agenda

  • Introduction
  • Access Modifiers
  • Operators
  • Flow Control
  • Arrays and Strings
  • OOPS Explored
  • Exceptions
  • Garbage Collection
  • Collections
  • Threads
  • Demo

Core Java

Introduction – Java Virtual Machine

.class file Java Virtual Machine .java file Java Compiler Mac Microsoft UNIX

Introduction – My First Program Version 1

package com.sharadballepu.test;

public class HelloWorld

public static void main(String[] args)

System.out.println(“Hello World”);

Compile the program: javac HelloWorld.java

Execute the program: java HelloWorld

Output: Hello World

package com.sharadballepu.test; public class HelloWorld { public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.display(args[0]); } public void display(String userName) { System.out.println(“Hello ” + userName); } } Compile the program: javac HelloWorld.java Execute the program: java HelloWorld Sharad Output: Hello Sharad

Introduction – My First Program Version 3

package com.sharadballepu.test; public class HelloWorld { String userName; public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.userName = args[0]; } public void display() { System.out.println(“Hello ” + userName); } } Compile the program: javac HelloWorld.java Execute the program: java HelloWorld Sharad Output: Hello Sharad

Introduction – My First Program Version 4

Introduction – Stack v/s Heap

x = 10 y = new A() method2() method1() main() Stack Heap

A

B

C

  • Class
    • A blueprint that defines the attributes and methods
  • Object
    • An instance of a Class
  • Abstraction
    • Hide certain details and show only essential details
  • Encapsulation
    • Binding data and methods together
  • Inheritance
    • Inherit the features of the superclass
  • Polymorphism
    • One name having many forms

Introduction - Object Oriented Concepts

13

Java Modifiers

Modifier Class Class

Variables

Methods Method

Variables

public ^ ^  private ^  protected ^  default ^ ^  final ^ ^ ^  abstract ^  strictfp ^  transient  synchronize d  native  volatile  static ^ ^ 

Modifiers – Class

  • public
    • Class can be accessed from any other class present in any package
  • default
    • Class can be accessed only from within the same package. Classes outside the package in which the class is defined cannot access this class
  • final
    • This class cannot be sub-classed, one cannot extend this class
  • abstract
    • Class cannot be instantiated, need to sub-classs/extend.
  • strictfp
    • Conforms that all methods in the class will conform to IEEE standard rules for floating points

16

Modifiers – Methods

  • public
    • Method can be accessed from any other class present in any package
  • private
    • Method can be accessed from only within the class
  • protected
    • Method can be accessed from all classes in the same package and sub- classes.
  • default
    • Method can be accessed only from within the same package.
  • final
    • The method cannot be overridden
  • abstract
    • Only provides the method declaration
  • strictfp
    • Method conforms to IEEE standard rules for floating points
  • synchronized
    • Only one thread can access the method at a time
  • native
    • Method is implemented in platform dependent language
  • static
    • Cannot access only static members.
  • Definition: An operator performs a particular operation on the operands it is applied on
  • Types of operators
    • Assignment Operators
    • Arithmetic Operators
    • Unary Operators
    • Equality Operators
    • Relational Operators
    • Conditional Operators
    • instaceof Operator
    • Bitwise Operators
    • Shift Operators

Operators - Types

  • Unary Operators Operator Description Example + Unary plus int i = +1; - Unary minus int i = -1; ++ Increment int j = i++; -- Decrement int j = i--; ! Logical Not boolean j = !true;

Operators – Unary Operators/Equality Operators

Operator Description Example == Equality If (i==1) != Non equality If (i != 4)

  • Equality Operators
  • Relational Operators Operator Description Example > Greater than if ( x > 4) < Less than if ( x < 4)

= Greater than or equal to if ( x >= 4) <= Less than or equal to if ( x <= 4)

Operators – Relational Operators/Conditional Operators

Operator Description Example && Conditional and If (a == 4 && b == 5) || Conditional or If (a == 4 || b == 5)

  • Conditional Operators