Sie sind auf Seite 1von 8

Lecture #2 Contents

Fundamental Programming  Data Types


Structures  Variables
 Operators
 Arrays
Instructor:
 Control Flow
Khalid Raza
Department of Computer Science  Classes & Objects
Jamia Millia Islamia, New Delhi, India  Inheritance
Email: kraza@jmi.ac.in | Home Page: www.kraza.in
 Assignments

Instructor: K. Raza

Data Types Data Types Contd


Data Storage Range Misc.
 Java is a strongly typed language. Types Requirements

 Java is a case-sensitive language. byte 1 byte -128 to 127 I


N
 There are eight primitive data types in short 2 bytes -32,768 to 32,767
T
E
Java. int 4 bytes -2,147,483,648 to 2,147,483,647
(Over 2 million) G
 Unicode support long 8 bytes -9,223,372,036,854,775,808 to E
9,233,372,036,854,775,807 R
float 4 bytes 3.40282347E+38F
(6-7 significant decimal digit) FLOATING
double 8 bytes 1.79769313486231570E+308 POINT VALUE
(15 significant decimal digits)
char 2 bytes Supports almost all Unicode CHARACTERS
Characters

boolean Machine Logical TRUE or FALSE value LOGICAL


Dependent
Instructor: K. Raza Instructor: K. Raza
Variables Operators
 A variable name must begin with a letter  Arithmetic Operators (+,-,*,/,%)
 Java reserve words cannot be used for a
variable name.  Increment/Decrement Operator (++,--)
 e.g. int i, j; // both are integers
int vacationDays=12; // initialization  Relational Operator (>, <, >=,<=, !=,==)
 General Syntax
<<premitive datatype>> <variable-name1>,
 Logical Operator (|| OR, && AND, !)
<variable-name2>, <variable-name2>;
 Bitwise Operator (&, |, ^, ~)
 Constants
final double pi=3.14;

Instructor: K. Raza Instructor: K. Raza

Operators Contd.. Operators Contd


Examples,  Other Aspect of Operators
x=x+5 or x+=5 (arithmetic operator)  Unary Operator (-, !)

 Binary Operator (-, +, *, /, %, >, < etc.)


int n=14;
 Ternary Operator
n++; (increment operator)
condition? expression1 : expression2;

3==4; (relational operator) e.g. x > y? x : y;

Instructor: K. Raza Instructor: K. Raza


Scope & Life of variables Scope & Life of variables contd..
e.g. class scope {
 A block ({}) defines a scope.
public static void main(String args[]){
int x; //known to all code within main
 Variable declared inside a scope are not x=10;
visible (i.e. not accessible) to code that is if(x>=10) { //start new scope
int y=20; // known only to this block
defined outside that scope.
//x and y both are known here
System.out.println(x + + y);
}
//y=10; Error! y is not in scope
// x is still in scope
System.out.println(x);
}
Instructor: K. Raza } Instructor: K. Raza

Conversion Between Data Types Type Casting


char  Casting
Number conversions in which loss of
information is possible are done by means
byte short int long of Casts.

e.g. double x=9.997;


int nx= (int) x;
float double
e.g. int n=12345678;
float f=n; //f is 1.23456792E8

Instructor: K. Raza Instructor: K. Raza


Arrays Multi Dimensional Arrays
 An array is a data structure that stores a  Java has no multidimensional arrays at all,
collection of values of the same type. only one dimensional array.
 Each individual values is accessed through  Multidimensional arrays are faked as
an integer index. array of arrays.
 e.g.
int[] a=new int[100]; //array of size 100 balances[1]= balances[1][3]=
balances=
.
.
.

Instructor: K. Raza Instructor: K. Raza

Ragged Array Input & Output


 An array in which difference rows have  To read console input, first construct Scanner
different length. that is attached to the standard input stream,
System.in
 Scanner is available in util package
1
1 1 Scanner in=new Scanner(System.in);
1 2 1 System.out.println(Whats your name?);
1 3 3 1 String name=in.nextLine();
System.out.println(Whats your age?);
1 4 6 4 1
int age=in.nextInt();

Instructor: K. Raza Instructor: K. Raza


Control Statements Selection Statements
if (condition)
 Selection Statements statement;
switch (expression){
case value1:
 if-else else //statement;
statement; break;
 switch
case value2:
 Iteration Statements OR //statement;
 while break;
if (condition) .
 do-while statement; .
else if(condition) .
 for
statement; case valueN:
 for-each . //statement;
. break;
. default:
else //default
statement; statement;
Instructor: K. Raza }
Instructor: K. Raza

Iterative Statements Class Fundamentals


while(condition) {  Class is the logical construct upon which the
//body of loop
} entire Java language is based.
for(init;condition;iteration){
//body
}  A class defines a new data type. Once a class is
defined, it can be used to create objects.

do { for(variable:collection){
//body of loop
 Class provides mechanism to achieve data
//statement;
} while(condition); //used for array navigation
encapsulation.
}

e.g. for(int element : a)


 An object is the instance of a class.
System.out.println(element);

Instructor: K. Raza Instructor: K. Raza


Defining a Class Defining a Class contd..
class ClassName { class Employee { class Employee {
constructor1; //constructor is optional //Constructor
public Employee(String n, double s){
constructor2; public String getName(){ public class EmployeeTest {
name=n;
return name; salary=s; public static void main(String args[]) {
method1; } } //creation of object & invoke constructor
method2; public double getSalary(){ public String getName() { Employee emp=new
return salary; return name; Employee(XYZ,40,000.00);

} //calling method through instance
field1; } public double getSalary() {
private String name; System.out.println(emp.getName());
field2; return salary;
System.out.println(emp.getSalary());
private double salary; }
private String name; }
} }
private double salary; }
}

Instructor: K. Raza Instructor: K. Raza

Initialization Block Method Overloading


 We have three ways to initialize a data field: class OverloadTest {
 By setting a value in a constructor void test() {
 By assigning a value in the declaration
 By using initialization block.
System.out.println(No parameters);
}
class Employee { void test(int a) {
public Employee() { System.out.println(a=+a); class OverLoad {
name=; public static void main(String args[]) {
} OverloadTest obj=new OverloadTest();
salary=0;
} void test(double a) { obj.test();
private int id, salary; System.out.println(a=+a); obj.test(10);
private String name; obj.test(10,20);
} obj.test(123.50);
{ void test(int a, int b){ }
Random generator=new Random();
id=generator.nextInt(10000);
System.out.println(a=+a);}
} System.out.println(b=+b);
} }
}
Instructor: K. Raza Instructor: K. Raza
Access Control Access Control Contd..
 public: Can be accessed from outside the private default protected public
program.
Same Class Yes Yes Yes Yes
 private: Can be accessed by the other Same package No Yes Yes Yes
members only. subclass
Same package non- No Yes Yes Yes
 protected: Allow accessibility to the
subclass
classes outside the current package but Different package No No Yes Yes
only to those who inherits it. subclass

 default: Member of the classes are public Different package No No No Yes


non-subclass
within its own package.

Instructor: K. Raza Instructor: K. Raza

static Keyword static methods


 static variable  Restriction of static methods
 Declares global variable.  They can only call other static methods.
 No copy of static variable is made.  They must only access static data.
 e.g. static int x=10;  They cannot refer to this or super.
 static method
 Can be accessed before objects of its class are
created.
 Can be accessed without reference of any
object.
 e.g. int x=Math.sqrt(45);

Instructor: K. Raza Instructor: K. Raza


Nested & Inner Classes Inheritance
class Outer {
int outer_x=100;  Superclass (Parent): A class that is
void test(){ inherited
Inner inner=new Inner();
inner.display();  Subclass (Child): A class that inherits
}
class Inner { other class. A subclass has all the
void display() { properties of superclass plus adds its own
System.out.println(display: +outer_x);
} properties.
}
}
Class InnerClassDemo {
public static void main(String args[]) {
Outer outer=new Outer();
outer.test();
}
}
Instructor: K. Raza Instructor: K. Raza

Inheritance contd..
class Employee {
public double getSalary() {
return salary;
}
private double salary;
}

class Manager extends Employee {


// properties of Employee is imported here
public void setBonus(double b) {
bonus=b;
}
private double bonus;
}
Instructor: K. Raza

Das könnte Ihnen auch gefallen