1.08k likes | 1.1k Views
Java programming. Prof.Rohini S Hanchate Pimpri Chinchawad polytechnic (PCP). Contents. Why java???? Java Introduction Java Features How Java Differs from other Object Oriented languages. Why java????. It’s entirely object-oriented
E N D
Java programming Prof.Rohini S Hanchate Pimpri Chinchawad polytechnic (PCP)
Contents • Why java???? • Java Introduction • Java Features • How Java Differs from other Object Oriented languages
Why java???? • It’s entirely object-oriented • It has a vast library of predefined objects and operations • It’s more platform independent • this makes it great for Web programming • It’s more secure • It isn’t C++
Java - An Introduction • Java - The new programming language developed by Sun Microsystems in 1991. • Originally called Oak by James Gosling, one of the inventors of the Java Language. • Java Authors: James , Arthur Van , and others • Java is purely object oriented.
Java Introduction • Originally created for consumer electronics (TV, VCR, Freeze, Washing Machine, Mobile Phone). • Java - Machine Independent language • Java is language of Internet Programming. • It allows you to publish a webpage with Java code in it.
Java Attributes • Familiar, Simple, Small • Compiled and Interpreted • Platform-Independent and Portable • Object-Oriented • Robust and Secure • Distributed • Multithreaded and Interactive • High Performance • Dynamic and Extensible
Java is Compiled and Interpreted Programmer Hardware and Operating System Source Code Byte Code Text Editor Compiler Interpreter .java file .class file java appletviewer netscape Notepad,emacs,vi javac
Compiled Languages Programmer Object Code Executable Code Source Code Text Editor Compiler linker .c file .o file a.out file Notepad,emacs,vi gcc
Total Platform Independence JAVA COMPILER (translator) JAVA BYTE CODE (same for all platforms) JAVA INTERPRETER (one for each different system) Windows 95 Macintosh Solaris Windows NT
Architecture Neutral & Portable • Java Compiler - Java source code(.java file) to byte code (file with .class) • Bytecode- an intermediate form, closer to machine representation • A interpreter interprets the bytecode.
Architecture Neutral & Portable • Porting the java system to any new platform any where any time. • The interpreter will figure out what the equivalent machine dependent code to run.
Rich Class Environment • Core Classes • language • Utilities • Input/Output • Low-Level Networking • Graphical User Interface • Internet Classes • TCP/IP Networking • WWW and HTML • Distributed Programs
Overlap of C, C++, and Java C++ C Java
Java better than C++ ? • No Typedefs, Defines, or Preprocessor • No Global Variables • No Goto statements • No Pointers • No Unsafe Structures • No Multiple Inheritance • No Operator Overloading
Simple java program Class first_program { Public static void main(String args[]) { System.out.println(“hellow world”); } }
Simple java program cntd... Public: The keyword public is an access specifies that declares the main method as unprotected. Static: Which declares main method as one that belongs to the entire class ,The main must always declare as static since the interpreter uses this method before any objects are created. If static keyword is specified then it is invoked first and no object creation is done for method Void: The type modifier void states that the main method does not return any value. Main() it is method to
JAVA Tokens • Smallest individual units in a program are known as Tokens. • Five different types of tokens: • Reserved Keywords. (60 Keywords, false, null, true, package etc.,). • Identifiers (alphabets, digits and underscore and dollar sign characters.) • Literals. (Integer, Floating Point. Character, String, Boolean) • Operators. • Separators.
Tokens cntd.... (Keywords) • Java language is having reserved 50 word as keywords.
Tokens cntd.... (identifiers) These are programer designed tokens. • Classes. • Methods • Variables. • Objects • Labels • Packages • Interfaces
Identfiers cntd... Rules for identifiers: • Alphabets,digits,underscore(_)and $ are allowed. • They must not begin with digit. • Uppercase and lowercase letters are distinct. • Identifier can be of any length. • Identfiers must be meaningful. • Short, easy, quick. • Easily readable and discriptive. Eg: average, dayTemerature,first_name,TOTAL.
Literals Literals are sequence of characters. 5 types of literals • Integer literals • Floating point literals • Charester literals • String literals • Boolean literals
Operators An operator is symbol that takes one or more arguments and operates on them to produce results.
Separators Symbols used to indicate where groups of code are divided and arranged.
Costants Constant in java refered to fixed values that do not change during execution of a program. • Numeric constants • Integer constants • Real constants • Charecter constants • Charechter constants • String constants
Constants cntd.... Integer constants: it refer as sequence of digits • Decimal integer constant consist of set of digit from 0 to 9 eg. 123 • Octal integer constants consist of from 0 to 7 eg. 037,0435 Real constants: numbers contain fractional parts refered to real constants eg 0.67878 Character constants: single character enclosed within a pair of single quotes eg. '4','a'. String constants: a sequence of charater encloused with in double qoutes eg. “hello”,”1998”.
Symboliuc constants • Symbolic names take the same form as variable names. • SC's are written in CAPITALS to distinguish them from normal variable names. • they should not be assigned any other value within the program after declaration of symbolic constants by using an assignment statement. eg. final int PEOPLE = 100; //we can not change the value. • This is not done in C and C++ where it is defined using the #define statement. • Can not be declared inside a method.
Variables Variable is an idenitfier that denotes a storage location used to store data value. • Variable may take different value at different times during execution of the program • Variable name should be Meaningful . • Variable must not begin with a digit. • Uppercase and lower case letters are distinct. • It should not be keyword. • White spaces is not allowed • Variable name can be anything.
Declaration of variables Variable are names of storage locations. After designing suitable variable names we must declare them to the compiler Why declaration????? • It tells the variable name to compiler • Type of data that variable holding • Place of variable decides scope of the variables • Variable must be declared before it used in program • Variable used to store value of any data type.
Declaration of variable cntd… Typevariable1,variable2,variable3…….; • Variables are separated by comma’s (,) • Declaration statement should end with ; . Eg , int count; float area; double p1; char c1,c2,c3;
Assigning values to variables • Using assignment statement Variable_Name = value; EgintialValue=0; finalValue=100; • String assignment eg x=y=z=0; • Assign values at declaration time EgintfinalValue=100; Char yes = ‘x’;
Scope of variable • Instance variable • Class variable • Local variable • Instance and class variables are declared inside a class. • Instance variables are created when the object are initiated and those variables are associated with the object. • Instance variable take different values for each object
Class variables are global to class. • Belongs to objects that class creates • Only one memory location is created for class variables. • Variables used and defined inside the method are local variables • Not available to outside the method definition.
Eg :statement1; { int x=0; \\ available to all { int n=5; \\ only with in braces } Statment2; { int m=20; \\ local variable } }
Type casting • Want store variable of one type in into a variable of other type. Type variable1 = (type) variable2; • It is converting one data type to another is known as casting. Eg int m= 12; byte n =(byte) m; Long count = (long) m;
Data types • Every variable is having Data type • Data type specify size and type of values that can be stored. • Depend on need of application select appropriate data type Category of Data Types • Primitive type • Derived type
Integer types Java support 4 types of integers.
DT cntd.. Character type • To store character constants in memory. • Size is 2 bytes Boolean type • To test particular condition we use Boolean type. • Only two values it takes True & False. • All comparison operators return Boolean type.
Standard default values • Every variable has default values.
Reading data from Keyboard Import java.lang.*; Import java.io.DataInputStream; Class read { public static void main (String args []) { DataInputStream in = new DataInputStream (System. in) int num = 0; System.out.println (“Enter a number :”); num = Integer.parseInt (in.readLine () ); System.out.println (“entered number Num = “ +num); } }
Output: Enter a Number: 123 entered number Num = 123
Operators Arithmetic operators
Relational operators: Compare two quantities and depend on their relation take certain decisions.