Vous êtes sur la page 1sur 29

Java Language Fundamentals

Session 2

Java Language Fundamentals / 1 of 28

Objectives

Interpret the Java program Understand the basics of the Java language Identify data types

Java Language Fundamentals / 2 of 28

Java Program Structure


Environment Information Class Declaration Tokens:


Identifiers Keywords / Reserve words Separators Literals Operators


Java Language Fundamentals / 3 of 28

Sample Java program

Java Language Fundamentals / 4 of 28

Compiling and executing the sample program

Java Language Fundamentals / 5 of 28

Passing Command Line Arguments (1)

Java Language Fundamentals / 6 of 28

Passing Command Line Arguments (2)

Java Language Fundamentals / 7 of 28

Basics of the Java Language

Classes & Methods


Data types Variables Operators Control structures

Java Language Fundamentals / 8 of 28

Classes in Java

Class declaration Syntax


class Classname { var_datatype variablename; : met_datatype methodname(parameter_list) : }

Java Language Fundamentals / 9 of 28

Sample class

Java Language Fundamentals / 10 of 28

Nested Classes

Defining one class within another is called Nesting

Types:

Static Non-static

Java Language Fundamentals / 11 of 28

Data Types

byte char boolean short int long float double

Array

Class
Interface

Java Language Fundamentals / 12 of 28

Type Casting

One data type is converted into another data type Example


float c = 34.89675f; int b = (int)c + 10;

Java Language Fundamentals / 13 of 28

Variables

Three components of a variable declaration:


Data type Name Initial value to be assigned (optional)

Syntax
datatype identifier [=value][, identifier[=value]...];

Java Language Fundamentals / 14 of 28

Java Reserve words

Java Language Fundamentals / 15 of 28

Array Declarations

Three ways for Array Declaration:

datatype identifier [ ]; datatype identifier [ ] = new datatype[size]; datatype identifier [ ]= {value1,value2,.valueN};

Java Language Fundamentals / 16 of 28

Methods in Classes

A method is defined as the actual implementation of an operation on an object Syntax


access_specifier modifier datatype method_name(parameter_list) { //body of method }

Java Language Fundamentals / 17 of 28

Sample usage of Method

Java Language Fundamentals / 18 of 28

Access specifiers for Methods

Java Language Fundamentals / 19 of 28

Method Modifiers

static abstract

final
native synchronized volatile
Java Language Fundamentals / 20 of 28

Methods Overloading

Overloaded Methods:

Are in the same class Have the same name Have different parameter lists

Overloaded methods are a form of compile time polymorphism

Java Language Fundamentals / 21 of 28

Methods Overriding

Overridden Methods:

Are in the superclass as well as the subclass Are redefined in the subclass

Overridden methods are a form of runtime polymorphism

Java Language Fundamentals / 22 of 28

Class Constructors

Special method used to initialize member variables of the class Same name as the Class name and does not have a return type Called when an object is created Types:

Explicit constructors Implicit constructors


Java Language Fundamentals / 23 of 28

Derived class constructors

Has the same name as the subclass


Statement that calls the constructor of the superclass should be the first statement in the constructor of a subclass

Java Language Fundamentals / 24 of 28

Operators
Java Provides following categories of Operators: Arithmetic Operators Bitwise Operators Relational Operators Logical Operators Conditional Operators Assignment Operators

Java Language Fundamentals / 25 of 28

Operator Precedence

Java Language Fundamentals / 26 of 28

Escape Sequence

Java Language Fundamentals / 27 of 28

Control Flow

Decision making

if-else statement Switch-else statement

Loops

while loop do-while loop for loop

Java Language Fundamentals / 28 of 28

Want to contact Unit Nexus


Unit Nexus Zirakpur Official Website Like Unit Nexus Pvt. Ltd. On Facebook Add Unit Nexus to your professional Network on Linked In Company Profile of Unit Nexus

Java Language Fundamentals / 29 of 28