Java Programming

Category: Education

Presentation Description

No description available.


Presentation Transcript

JAVA Programming :

JAVA Programming ---DevanRaju Pericherla

PowerPoint Presentation:

We are learning this language with JAVA Character Set. As JAVA is a universal language, it not only supports characters in english, but also supports characters in another languages. JAVA uses UNICODE character set. UNICODE stands for universal code. UNICODE is a superset of ASCII Code. In JAVA each character occupies 2 bytes of space. UNICODE contains 65536 characters.

Tokens in JAVA:

Tokens in JAVA Smallest individual units in a program is known as Tokens. They are:- Keywords Identifiers Constants Operators DataTypes.

PowerPoint Presentation:

JAVA reserved words are divided in to two types they are:- Keywords Reserved literals.

(1) Keyword (def):

(1) Keyword (def) Keyword is a language related instruction. (or) Keyword is a pre-defined instruction. (or) Keyword is a word which has a special meaning in that language, which meaning can not be changed.

PowerPoint Presentation:

Keywords are classified in to two types Used keywords. Unused keywords. Goto and Const are Unused Keywords,not given to the programmer but internally JAVA uses these keywords as they are unstructured. They are 48 used keywords.

Reserved Literals:

Reserved Literals True, false, null are reserved literals. So, totally 58 reserved words are there in JAVA. ---In-detail

PowerPoint Presentation:

Int, float, double, long, character, boolean, void, short and bye are the keywords for datatype. Private, public, protected, final, static, abstract, transient, volatile, synchronized are the keywords for modifiers. Try,catch,finally,throw,throws are the keywords for exception handling. If, else, which, for, do, break, continue, switch, case are the keywords for control structures . Class, interface, extends, implement, package, import are the keywords for class.

PowerPoint Presentation:

New, instance of, super, this are the keywords for an object. In JAVA 1.4 they introduced a new keyword “assert” . In JAVA 1.5 they introduced a new keyword called “enum”.

(2) Identifiers:

(2) Identifiers Identifier is a user defined word, in order to identify data (or) set of instructions. Ex:- Int x, class student x, student are the variable name, class name or identifiers.

Rules for an Identifiers:

Rules for an Identifiers An identifier is a collection of characters (alphabets), digits and special characters($,_). Variable name should start with alphabet and should start with lower case letter only. If the variable having multiple words then it must be separated by a capital letter. Ex: - accName. Function name should starts with small letter, if function name having multiple words,seperate with capital letters. Ex: - printDetails();

PowerPoint Presentation:

Constant name should be defined in capital letters.if constant name,having multiple words, then it must be separated by _ (underscore) Ex :-PI, MIN_VALUE, MAX_VALUE.


JAVA DATATYPES A datatype, which reserves the memory for the variable. They are two types of datatypes. 1) Primitive Datatype. 2) Reference Datatype. In “C” reference means giving of address, but in JAVA reference holds (or) holds the value. In “C” we have call by value and call by reference, but in JAVA we have only call by Value.

PowerPoint Presentation:

Primitive data types are: Byte (1 byte) Int (4 bytes) Short (2 bytes) Long (8 bytes) Float (4 bytes) Double (8 bytes) Boolean (1 byte) Void (0 bytes) Char (2 bytes) We don’t have any signed or unsigned types in JAVA. Size of datatypes does not changes as java is architectural neutral

PowerPoint Presentation:

The reference datatypes are Class. Interface. Enum. Array. Reference datatypes is a restricted pointer variables. Size of datatypes does not changed, when app moved from one O.S to another O.S. Primitive datatype holds the value and reference holds the address.

Structure of a JAVA program:

Structure of a JAVA program JAVA program is divided in to 4 sections, they are:- Document section. Package section. Import section. Class section. All the sections are optional. We have to follow the same sequence, you can not interchange.

PowerPoint Presentation:

Document section:- This section contains the information about the JAVA program. In JAVA documentation is provided using comments. Java supports 3 types of comments Multi line comment(/*..........*/) Single line Comment (//....) Documented Comment(/**...........**/ )

PowerPoint Presentation:

Documented comment:- It is a multi lined comment, which can be separated from source program, inorder to build a document file. This document file is in HTML format. Documented Comments can be separated from the source program using a tool called “Javadoc”.

PowerPoint Presentation:

Package Section:- Package is a container, which encapsulates with set of classes. In this section we will specify the user defined packages. If you specify the classes in a package, it has access protection(modularity)rather than class in public and we can use the class directly with the class name. A package which contain, set of types also.

PowerPoint Presentation:

Import section: - Import is not including but adding fully qualified name. (or) Importing is nothing but navigating or adding the reference of the class. Here fully qualified name is a class name along with package name(directory). Ex:- Package where all the classes are encapsulated.

PowerPoint Presentation:

Class section:- JAVA is a pure Object-Oriented, so every thing in JAVA is encapsulated within a class. In core java/JSE applications are (1) Standalone programs (2) Applet.

PowerPoint Presentation:

Standalone Programs:- Standalone program or console based program must be at least one main method and which will be executed by the end user. Applet:- Applet is a internet program or a web content, which is embedded within HTML/webpage. It is downloaded along with webpage and executed within browser. So, applet does not have main method Ex:- A welcome.html page having Applet login Window.

First program in JAVA:

First program in JAVA IDE(Integrated Development Environment) is the environment where we can compile and execute the JAVA complier. JAVA IDE'S are:- (1) MyEclipse. (2) NetBeans. (3) Jbuilder's

PowerPoint Presentation:

As we are beginners, we are using Notepad in windows OS for writing a JAVA program. After typing the JAVA program save the program with .java extension. Ex:- “”. (“ ” are must) For compiling this program in command prompt, go to the that directory where you have saved this JAVA program, and use this command “javac”. By this statement java complier invokes and generates .class files(nothing but ByteCode). If your java program contains 5 classes, then complier will generates 5 different class files with class names.

PowerPoint Presentation:

So, every class in a Java Source program, java complier generates “.class file”. Now, we will interpret and execute this JAVA program with the help of a command “ java classname”. (class name which contains the main method). JVM invokes by this command and it will loads from hard disk to RAM and then it interprets. If you specify the class(which contains main method) as public, then JAVA program must be saved with the class name only. JVM will only understand one type of main method called “public static void main”.

PowerPoint Presentation:

Class demo { Public static void main(string args[ ]) { } } Save the program “”. Compiling the program is javac Running the program is java demo. Note :- if u declare the class as public, then you have to save the program with class name only.

PowerPoint Presentation:

Syntax of main method:- Public static void main(String args[ ]) { }. Public static void main(String[ ] args){ }. Static public void main(String args[ ]) { }. Public static void main(String... args){ } //this syntax is supported by JAVA1.5 or above.

Why main() is “public”:

Why main() is “public” All the classes are encapsulated in a container called package. In order to use the classes in this package by the outside world, then the class containing main method must be public. So, if you are not specifying as “Public” to the main method then, that package will be useless for accessing outside.

Why main() is Static:

Why main() is Static Static methods bind with class name. Static members does not require to create object, as we can directly called with the class name. If you are not specifying “static”, then we have to create the object for the class and call the main method with object. Ex:- java classname Classname.main() will be called automatically.

Why main() is void:

Why main() is void Main() is called from O.S. The values which are send from O.S are type string. The values which are send from the command line(O.S) to main method are called command line arguments. So, main(String args[]) are command line arguments. All these arguments are optional.

PowerPoint Presentation:

Public static void main(String args[ ]) here Public is a Access Specifier. static is a Access identifier. void is a return type. Main() is a name of a method String is a predefined class in java.lang Args[ ] is a name of the parameters of type string.

Printing methods in JAVA:

Printing methods in JAVA They are various method, they are (1) System.out.println (2) System.out.print (3) System.out.printf (it is introduced in JAVA 1.5)


System.out.println System is a class available in Java.lang package. System class provides two standard input & output operations. Print-stream class is a class that provides, set of methods to perform output operations. Println is a method in print-stream class. Out is a static reference or object of a print-stream class. Out is pointing to an object of print-stream class. So, we are calling “println method” with the help of the object “out” in System class.

PowerPoint Presentation:

Println is a method in print-stream class, which prints in new line. This method is overloaded in print-stream class in-order to print different types of values. Total there are 11 types of println() method s are there in print-stream class. (8 are primitive data types, 1 is blank line, 1 is for printing string, 1 is printing object.) One println can print only one value, because it was having only one argument.


System.out.print This method is for printing in same line. Print() method is overloaded in print stream class in-order to print various types of values. System.out.print(10) //integer value. System.out.print(1.5) //double value. System.out.print(1.5f) //float value.


System.out.printf Just like in 'C', from JAVA 1.5 we have this statement:- System.out.printf(“%d”,10) System.out.printf(“%d %d”,10,20) System.out.printf(“%s %f”,“Java”,1.5f) System.out.printf(“%d%f%c”,10,1.5f,'a') By using this statement we can print more than one value, whereas by using System.out.print() and System.out.println() we can print only one value.

PowerPoint Presentation:

To overcome this problem we are using the concept of concatenation by using '+' operator. '+' is a operator with overloaded with two operations (1) normal addition (2) for concatenation of two strings. JAVA does not support operator overloading, but there are some operators overloaded in java like '+'. (operator overloading is not given to the programmers) For performing concatenation at least one operand must be string.

PowerPoint Presentation:

10+20=30 (normal addition) 10+“20”=10 20 (concat os strings) “10”+20=10 20 (concat os strings) “10”+“20”=10 20 (concat os strings) Ex:- Int x=10; Int y=20; System.out.println(“sum is” + x+y); // sum is 30 System.out.println(x+y + “is sum”); // 30 is sum. '+' is concatenating the string and the values.

PowerPoint Presentation:

Int x=10; int y=20; System.out.println(x+“and”+y); Then the output will be 10 and 20. In this way we can print two values in System.out.println statement by using concatenation. If you use System.out.println(x,y) which leads to error.

Local Variables:

Local Variables Variables declared within a method are called local variables. Local variables must be assigned to a value, before accessing it. There is no default value assigned to a local variable Ex:- int x; System.out.printf(“%d”,x); Not allowed in JAVA

Type Conversion:

Type Conversion Converting of one type of variable to another type is called type conversion (here type is datatype) Why type conversion? Byte x=10*30; gives the error because, the result of this exceeding the range of a byte. Float x=10/7; gives the out put 1(as integer divided by integer gives integer), but we declare the x type as float. So there is a great need of type conversions.

PowerPoint Presentation:

Typing conversions are of two types (1) Narrowing conversion (2) Widening conversion. Narrowing conversions are of two types (1) narrowing primitive conversion (2) narrowing reference conversion. Widening conversion are of two types (1) widening primitive conversions (2) widening reference conversions. In order to do the type conversion we must know the hierarchy of datatypes

PowerPoint Presentation:

Hierarchy of datatypes in bytes:- (higher to lower) Double Float Long Int Short Char Byte

PowerPoint Presentation:

From top to bottom data type conversion is called down casting or explicit or narrowing conversion. (or) Converting of border datatype to narrower type is called narrowing conversion. In this conversion, there is a chance of loss of value, because it may not fit. Ex:- int x=65; byte b=(byte)x; float a=1.5f; int b=(int)a;

PowerPoint Presentation:

From bottom to top data type conversion is called upcasting or implicit or widening conversion . Converting of narrow datatype to boarder datatype is called widening conversion . Converting os narrow primitive type to broader primitive type called widening primitive conversion or upcasting. The conversions are: Int to long,float,double. Short to int,long,float,double Long to float,double Char to short, int,long Float to double Byte to short,int,long,float, double

PowerPoint Presentation:

Remaining java fundamentals and programming in next ppt....

authorStream Live Help