UNIT 2 PROGRAMMING IN JAVA • JAVA INTRODUCED BY JAMES GOSLING • Designed for development of software for devices like tv, vcr and other electronic machines • It is owned by Oracle • More than 3 billion devices run java • Java is object oriented language • Java is fast, secure and reliable. Why use java? • Java works on different platforms • Large demands in the current market. • Open source and free JAVA PROGRAMMING ENVIORNMENT JAVA PROGRAM (PROGAM NAME.java) Byte code(.class file generated) javac compiler JAVA RUN TIME(JRE) JVM Libraries+ classes JDK-JAVA DEVELOPMENT KIT JAVA PROGRAMMING ENVIORNMENT JAVA PROGRAM STRUCTURE Document section// Optional Package section// optional Import statement Interface statement// optional Class definition Main method class • It is a user-defined blueprint or prototype from which objects are created. • A class is a group of objects which have common properties. objects • An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. • Objects are the instances of a class. • An object consists of : • State: It is represented by attributes of an object. It also reflects the properties of an object. • Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects. • Identity: It gives a unique name to an object and enables one object to interact with other objects. Class and Objects How to declare a class??? • • Class is a group of variables of different data types and a group of methods. A Class in Java can contain: – Data member – Method – Constructor – Nested Class – Interface How to declare a class??? class classname { Data members; Methods; } How to create a objects?? • • • • • Using a new keyword Using the newInstance() method of the Class class Using the newInstance() method of the Constructor class Using Object Serialization and Deserialization Using the clone() method Using a new keyword • Syntax: Classname objectname=new classname(); cake c=new cake(); cake c1=new cake(); Cake c2=new cake()’ Using a new keyword DATATYPES IN JAVA First program in java!!! class D// Class Declaration { public static void main(String args[])// mainmethod { System.out.println(“Hi”); // statement } } To save: To compile: To run: D.java javac D.java java D First program in java!!! Scanner Class- to get input from user Programs to be practiced • Addition of two numbers • Square root of quadratic equation Operators ARITHMETIC OPERATORS ASSUME A=10, B=20 Assignment Operators Relational Operators Logical Operators Unary Operator Bitwise Operator class JavaOperators { public static void main(String[] args) { int a = 58; //111010 int b=13; //1101 System.out.println(a&b); //returns 8 = 1000 System.out.println(a|b); //63=111111 System.out.println(a^b); //55=11011 System.out.println(~a); //-59 } } Ternary Operators (Condition) ? (Statement1) : (Statement2); Int a = 20, b = 10, res; res = ((a > b) ? a: b ); Structured Programming If - statement • It makes one way based on single condition Syntax: if(condition) { Statement; } If – statement - Example • • • • If(a>b) { System.out.println(“Condition is true”); } If-else • if..else statements is a two way statement. Based on the condition, it follows any one of the two ways general form of if…else statement if(condition) { Statements; } else { Statements; } If – statement - Example • • • • If(a>b) { System.out.println(“Condition is true”); } If-else • if..else statements is a two way statement. Based on the condition, it follows any one of the two ways general form of if…else statement if(condition) { Statements; } else { Statements; } If-else example: if(x<10) { System.out.println(“Condition is true”); else { System.out.println(“Condition is false”); } } Switch statements • Switch is a multi-way branching statement Switch statement Syntax: Switch(<expression>) { Case<constant 1>: <statement> [break;] Case<constant 2>: <statement> [break;] default: <statement > [break;] Switch statement Example: int a=2; Switch (a) { case 1: System.out.println(“1”); break; case 2: System.out.println(“2”); break; } Looping Statements • ITERATIVE STATEMENTS: Statements to again; • For loops syntax: for(<initialization>;<condition>;<increment/dectrement>) • { • • <statement block> } executed again and Looping STATEMENTS While loop syntax: while(<test exprs>) <statement> Example: int i=0; while(i<10) { System.out.println(i+ “squared is” + (i*i)); i++; } do-while do { // Statements }while(condition); { Statement; }