what is java programming language and lecture notes advanced java programming | pdf free download
DanialOrton Profile Pic
Published Date:09-07-2017
Your Website URL(Optional)
Java Programming LECTURE NOTES ON JAVA POROGRAMMING MCA II YEAR, I SEMESTER (JNTUA-R09) Mr.D.Surendra Asst.Professor DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS CHADALAWADA RAMANAMMA ENGINEERING COLLEGE CHADALAWADA NAGAR, RENIGUNTA ROAD, TIRUPATI (A.P) - 517506 Java Programming JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR. MCA II-I Sem Th 4 (9F00305) JAVA PROGRAMMING UNIT I Java Basics - History of Java, Java buzzwords, comments, data types, variables, constants, scope and life time of variables, operators, operator hierarchy, expressions, type conversion and casting, enumerated types, control flow-block scope, conditional statements, loops, break and continue statements, simple java program, arrays, input and output, formatting output, Review of OOP concepts, encapsulation, inheritance, polymorphism, classes, objects, constructors, methods, parameter passing, static fields and methods, access control, this reference, overloading methods and constructors, recursion, garbage collection, building strings, exploring string class, Enumerations, autoboxing and unboxing, Generics. UNIT II Inheritance – Inheritance concept, benefits of inheritance ,Super classes and Sub classes, Member access rules, Inheritance hierarchies, super uses, preventing inheritance: final classes and methods, casting, polymorphism- dynamic binding, method overriding, abstract classes and methods, the Object class and its methods. Interfaces – Interfaces vs. Abstract classes, defining an interface, implementing interfaces, accessing implementations through interface references, extending interface. UNIT III Inner classes – Uses of inner classes, local inner classes, anonymous inner classes, static inner classes, examples. Packages-Defining, Creating and Accessing a Package, Understanding CLASSPATH, importing packages. UNIT IV Data structures creation and manipulation in java – Introduction to Java Collections, Overview of Java Collection frame work, Commonly used Collection classes – ArrayList, LinkedList, HashSet, HashMap, TreeMap, Collection Interfaces – Collection, Set, List, Map, Legacy Collection classes – Vector, Hashtable, Stack, Dictionary(abstract), Enumeration interface, Iteration over Collections – Iterator interface, ListIterator interface. Other Utility classes – StringTokenizer, Formatter, Random, Scanner, Observable, Using java.util. UNIT V Files – streams- byte streams, character streams, text Input/output, binary input/output, random access file operations, File management using File class, Using java.io. Networking in Java – Introduction, Manipulating URLs, Ex. Client/Server Interaction with Stream Socket Connections, Connectionless Client/Server Interaction with Datagrams, Using java.net. UNIT VI Exception handling – Dealing with errors, benefits of exception handling, the classification of exceptions- exception hierarchy, checked exceptions and unchecked exceptions, usage of try, catch, throw, throws and finally, rethrowing exceptions, exception specification, built in exceptions, creating own exception sub classes, Guide lines for proper use of exceptions. Multithreading - Differences between multiple processes and multiple threads, thread states, creating threads, interrupting threads, thread priorities, synchronizing threads, interthread communication, thread groups, daemon threads. Java Programming UNIT VII : GUI Programming with Java - The AWT class hierarchy, Introduction to Swing, Swing vs. AWT,MVC architecture, Hierarchy for Swing components, Containers – Top-level containers – JFrame, JApplet, JWindow, JDialog, Light weight containers – JPanel, A simple swing application, Overview of several swing components- Jbutton, JToggleButton, JCheckBox, JRadioButton, JLabel, JTextField, JTextArea, JList, JComboBox, JMenu, Java‟s Graphics capabilities – Introduction, Graphics contexts and Graphics objects, color control, Font control, Drawing lines, rectangles and ovals, Drawing arcs, Layout management - Layout manager types – border, grid, flow, box. UNIT VIII Event Handling - Events, Event sources, Event classes, Event Listeners, Relationship between Event sources and Listeners, Delegation event model, Semantic and Low-level events, Examples: handling a button click, handling mouse and keyboard events, Adapter classes. Applets – Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet - Four methods of an applet, Developing applets and testing, passing parameters to applets, applet security issues.. REFERENCES : th 1. Java: the complete reference, 7 editon, Herbert Schildt, TMH. 2. Java for Programmers, P.J.Deitel and H.M.Deitel, Pearson education / Java: How to th Program P.J.Deitel and H.M.Deitel ,8 edition, PHI. 3. Core Java, Volume 1-Fundamentals, eighth edition, Cay S.Horstmann and Gary Cornell, Pearson eduction. 4. Java Programming, D.S.Malik, Cengage Learning. 5. Object Oriented Programming with Java, B.Eswara Reddy, T.V.Suresh Kumar, P.Raghavan, Pearson-Sanguine. 6. An introduction to Java programming and object oriented application development, R.A. Johnson- Cengage Learning. 7. Advanced Programming in Java2, K.Somasundaram, Jaico Publishing House. 8. Starting out with Java, T.Gaddis, dreamtech India Pvt. Ltd. 9. Object Oriented Programming with Java, R.Buyya, S.T.Selvi, X.Chu, TMH. 10. Object Oriented Programming through Java, P.Radha Krishna, Universities Press. 11. An introduction to programming and OO design using Java, J.Nino, F.A.Hosch, John Wiley&Sons. 12. Java and Object Orientation, an introduction, John Hunt, second edition, Springer. 13.Maurach‟s Beginning Java2,D.Lowe, J.Murach, A. Steelman, SPD. 14. Programming with Java, M.P.Bhave, S.A.Patekar, Pearson Education Java Programming 1. Introduction to Java History of Java: · In 1990, Sun Micro Systems Inc. (US) was conceived a project to develop software for consumer electronic devices that could be controlled by a remote. This project was called Stealth Project but later its name was changed to Green Project. · In January 1991, Project Manager James Gosling and his team members Patrick Naughton, Mike Sheridan, Chris Wrath, and Ed Frank met to discuss about this project. · Gosling thought C and C++ would be used to develop the project. But the problem he faced with them is that they were system dependent languages. The trouble with C and C++ (and most other languages) is that they are designed to be compiled for a specific target and could not be used on various processors, which the electronic devices might use. · James Gosling with his team started developing a new language, which was completely system independent. This language was initially called OAK. Since this name was registered by some other company, later it was changed to Java. · James Gosling and his team members were consuming a lot of coffee while developing this language. Good quality of coffee was supplied from a place called “Java Island‟. Hence they fixed the name of the language as Java. The symbol for Java language is cup and saucer. rd · Sun formally announced Java at Sun World conference in 1995. On January 23 1996, JDK1.0 version was released. Features of Java (Java buzz words): · Simple: Learning and practicing java is easy because of resemblance with c and C++. · Object Oriented Programming Language: Unlike C++, Java is purely OOP. · Distributed: Java is designed for use on network; it has an extensive library which works in agreement with TCP/IP. · Secure: Java is designed for use on Internet. Java enables the construction of virus- free, tamper free systems. · Robust (Strong/ Powerful): Java programs will not crash because of its exception handling and its memory management features. · Interpreted: Java programs are compiled to generate the byte code. This byte code can be downloaded and interpreted by the interpreter. .class file will have byte code instructions and JVM which contains an interpreter will execute the byte code. · Portable: Java does not have implementation dependent aspects and it yields or gives same result on any machine. · Architectural Neutral Language: Java byte code is not machine dependent, it can run on any machine with any processor and with any OS. · High Performance: Along with interpreter there will be JIT (Just In Time) compiler which enhances the speed of execution. · Multithreaded: Executing different parts of program simultaneously is called multithreading. This is an essential feature to design server side programs. · Dynamic: We can develop programs in Java which dynamically change on Internet (e.g.: Applets). 1 Java Programming Obtaining the Java Environment: · We can download the JDK (Java Development Kit) including the compiler and runtime engine from Sun at: http://java.sun.com/javase. · Install JDK after downloading, by default JDK will be installed in C:\Program Files\Java\jdk1.5.0_05 (Here jdk1.5.0_05 is JDK‟s version) Setting up Java Environment: After installing the JDK, we need to set at least one environment variable in order to able to compile and run Java programs. A PATH environment variable enables the operating system to find the JDK executables when our working directory is not the JDK's binary directory. · Setting environment variables from a command prompt: If we set the variables from a command prompt, they will only hold for that session. To set the PATH from a command prompt: set PATH=C:\Program Files\Java\jdk1.5.0_05\bin;%PATH% · Setting environment variables as system variables: If we set the variables as system variables they will hold continuously. o Right-click on My Computer o Choose Properties o Select the Advanced tab o Click the Environment Variables button at the bottom o In system variables tab, select path (system variable) and click on edit button o A window with variable name- path and its value will be displayed. o Don‟t disturb the default path value that is appearing and just append (add) to that path at the end: ;C:\ProgramFiles\Java\ jdk1.5.0_05\bin; o Finally press OK button. 2 Java Programming 2. Programming Structure Comments: Comments are description about the aim and features of the program. Comments increase readability of a program. Three types of comments are there in Java: · Single line comments: These comments start with // e.g.: // this is comment line · Multi line comments: These comments start with / and end with / e.g.: / this is comment line/ · Java documentation comments: These comments start with / and end with / These comments are useful to create a HTML file called API (application programming Interface) document. This file contains description of all the features of software. Structure of the Java Program: · As all other programming languages, Java also has a structure. · The first line of the C/C++ program contains include statement. For example, stdio.h is the header file that contains functions, like printf (), scanf () etc. So if we want to use any of these functions, we should include this header file in C/ C++ program. · Similarly in Java first we need to import the required packages. By default java.lang. is imported. Java has several such packages in its library. A package is a kind of directory that contains a group of related classes and interfaces. A class or interface contains methods. · Since Java is purely an Object Oriented Programming language, we cannot write a Java program without having at least one class or object. So, it is mandatory to write a class in Java program. We should use class keyword for this purpose and then write class name. · In C/C++, program starts executing from main method similarly in Java, program starts executing from main method. The return type of main method is void because program starts executing from main method and it returns nothing. Sample Program: //A Simple Java Program import java.lang.System; import java.lang.String; class Sample public static void main(String args) System.out.print ("Hello world"); · Since Java is purely an Object Oriented Programming language, without creating an object to a class it is not possible to access methods and members of a class. But main method is also a method inside a class, since program execution starts from main method we need to call main method without creating an object. · Static methods are the methods, which can be called and executed without creating objects. Since we want to call main () method without using an object, we should declare main () 3 Java Programming method as static. JVM calls main () method using its Classname.main () at the time of running the program. · JVM is a program written by Java Soft people (Java development team) and main () is the method written by us. Since, main () method should be available to the JVM, it should be declared as public. If we don‟t declare main () method as public, then it doesn‟t make itself available to JVM and JVM cannot execute it. · JVM always looks for main () method with String type array as parameter otherwise JVM cannot recognize the main () method, so we must provide String type array as parameter to main () method. · A class code starts with a and ends with a. A class or an object contains variables and methods (functions). We can create any number of variables and methods inside the class. This is our first program, so we had written only one method called main (). · Our aim of writing this program is just to display a string “Hello world”. In Java, print () method is used to display something on the monitor. · A method should be called by using objectname.methodname (). So, to call print () method, create an object to PrintStream class then call objectname.print () method. · An alternative is given to create an object to PrintStream Class i.e. System.out. Here, System is the class name and out is a static variable in System class. out is called a field in System class. When we call this field a PrintStream class object will be created internally. So, we can call print() method as: System.out.print (“Hello world”); · println () is also a method belonging to PrintStream class. It throws the cursor to the next line after displaying the result. · In the above Sample program System and String are the classes present in java.lang package. Escape Sequence: Java supports all escape sequence which is supported by C/ C++. A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. Escape Sequence Description \t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point. \r Insert a carriage return in the text at this point. \f Insert a form feed in the text at this point. \' Insert a single quote character in the text at this point. \" Insert a double quote character in the text at this point. \\ Insert a backslash character in the text at this point. Creating a Source File: · Type the program in a text editor (i.e. Notepad, WordPad, Microsoft Word or Edit Plus). We can launch the Notepad editor from the Start menu by selecting Programs Accessories Notepad. In a new document, type the above code (i.e. Sample Program). · Save the program with filename same as Class_name (i.e. Sample.java) in which main method is written. To do this in Notepad, first choose the File Save menu item. Then, in the Save dialog box: 4 Java Programming o Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is JQR on the D drive. o In the File name text field, type "Sample.java", including the quotation marks. Then the dialog box should look like this: o Now click Save, and exit Notepad. Compiling the Source File into a .class File: · To Compile the Sample.java program go to DOS prompt. We can do this from the Start menu by choosing Run... and then entering cmd. The window should look similar to the following figure. · The prompt shows current directory. To compile Sample.java source file, change current directory to the directory where Sample.java file is located. For example, if source directory is JQR on the D drive, type the following commands at the prompt and press Enter: Now the prompt should change to D:\JQR · At the prompt, type the following command and press Enter. javac Sample.java 5 Java Programming · The compiler generates byte code and Sample.class will be created. Executing the Program (Sample.class): · To run the program, enter java followed by the class name created at the time of compilation at the command prompt in the same directory as: java Sample · The program interpreted and the output is displayed. The Java Virtual Machine: Java Virtual Machine (JVM) is the heart of entire Java program execution process. First of all, the .java program is converted into a .class file consisting of byte code instructions by the java compiler at the time of compilation. Remember, this java compiler is outside the JVM. This .class file is given to the JVM. Following figure shows the architecture of Java Virtual Machine. Figure: The internal architecture of the Java virtual machine. In JVM, there is a module (or program) called class loader sub system, which performs the following instructions: · First of all, it loads the .class file into memory. · Then it verifies whether all byte code instructions are proper or not. If it finds any instruction suspicious, the execution is rejected immediately. 6 Java Programming · If the byte instructions are proper, then it allocates necessary memory to execute the program. This memory is divided into 5 parts, called run time data areas, which contain the data and results while running the program. These areas are as follows: o Method area: Method area is the memory block, which stores the class code, code of the variables and code of the methods in the Java program. (Method means functions written in a class). o Heap: This is the area where objects are created. Whenever JVM loads a class, method and heap areas are immediately created in it. o Java Stacks: Method code is stored on Method area. But while running a method, it needs some more memory to store the data and results. This memory is allotted on Java Stacks. So, Java Stacks are memory area where Java methods are executed. While executing methods, a separate frame will be created in the Java Stack, where the method is executed. JVM uses a separate thread (or process) to execute each method. o PC (Program Counter) registers: These are the registers (memory areas), which contain memory address of the instructions of the methods. If there are 3 methods, 3 PC registers will be used to track the instruction of the methods. o Native Method Stacks: Java methods are executed on Java Stacks. Similarly, native methods (for example C/C++ functions) are executed on Native method stacks. To execute the native methods, generally native method libraries (for example C/C++ header files) are required. These header files are located and connected to JVM by a program, called Native method interface. Execution Engine contains interpreter and JIT compiler which translates the byte code instructions into machine language which are executed by the microprocessor. Hot spot (loops/iterations) is the area in .class file i.e. executed by JIT compiler. JVM will identify the Hot spots in the .class files and it will give it to JIT compiler where the normal instructions and statements of Java program are executed by the Java interpreter. 7 Java Programming 3. Naming Conventions, Data Types and Operators Naming Conventions: Naming conventions specify the rules to be followed by a Java programmer while writing the names of packages, classes, methods etc. · Package names are written in small letters. e.g.: java.io, java.lang, java.awt etc · Each word of class name and interface name starts with a capital e.g.: Sample, AddTwoNumbers · Method names start with small letters then each word start with a capital e.g.: sum (), sumTwoNumbers (), minValue () · Variable names also follow the same above method rule e.g.: sum, count, totalCount · Constants should be written using all capital letters e.g.: PI, COUNT · Keywords are reserved words and are written in small letters. e.g.: int, short, float, public, void Data Types: The classification of data item is called data type. Java defines eight simple types of data. byte, short, int, long, char, float, double and boolean. These can be put in four groups: · Integer Data Types: These data types store integer numbers Data Type Memory size Range Byte 1 byte -128 to 127 Short 2 bytes -32768 to 32767 Int 4 bytes -2147483648 to 2147483647 Long 8 bytes -9223372036854775808 to 9223372036854775807 e.g.: byte rno = 10; long x = 150L; L means forcing JVM to allot 8 bytes · Float Data Types: These data types handle floating point numbers Data Type Memory size Range Float 4 bytes -3.4e38 to 3.4e38 Double 8 bytes -1.7e308 to 1.7e308 e.g.: float pi = 3.142f; double distance = 1.98e8; · Character Data Type: This data type represents a single character. char data type in java uses two bytes of memory also called Unicode system. Unicode is a specification to include alphabets of all international languages into the character set of java. Data Type Memory size Range Char 2 bytes 0 to 65535 e.g.: char ch = 'x'; · Boolean Data Type: can handle truth values either true or false e.g.:- boolean response = true; 8 Java Programming Operators: An operator is a symbol that performs an operation. An operator acts on variables called operands. · Arithmetic operators: These operators are used to perform fundamental operations like addition, subtraction, multiplication etc. Operator Meaning Example Result + Addition 3 + 4 7 - Subtraction 5 - 7 -2 Multiplication 5 5 25 / Division (gives quotient) 14 / 7 2 % Modulus (gives remainder) 20 % 7 6 · Assignment operator: This operator (=) is used to store some value into a variable. Simple Assignment Compound Assignment x = x + y x += y x = x – y x -= y x = x y x = y x = x y x /= y · Unary operators: As the name indicates unary operator‟s act only on one operand. Operator Meaning Example Explanation - Unary minus j = -k; k value is negated and stored into j b value will be incremented by 1 Increment b++; (called as post incrementation) ++ Operator ++b; b value will be incremented by 1 (called as pre incrementation) b value will be decremented by 1 Decrement b; (called as post decrementation) Operator b; b value will be decremented by 1 (called as pre decrementation) · Relational operators: These operators are used for comparison purpose. Operator Meaning Example == Equal x == 3 = Not equal x = 3 Less than x 3 Greater than x 3 Less than or equal to = x = 3 · Logical operators: Logical operators are used to construct compound conditions. A compound condition is a combination of several simple conditions. Operator Meaning Example Explanation if(ab && ac) If a value is greater than b and c && and operator System.out.print(“yes”); then only yes is displayed if(a==1 b==1) If either a value is 1 or b value is 1 or operator System.out.print(“yes”); then yes is displayed if( (a==0) ) If a value is not equal to zero then not operator System.out.print(“yes”); only yes is displayed 9 Java Programming · Bitwise operators: These operators act on individual bits (0 and 1) of the operands. They act only on integer data types, i.e. byte, short, long and int. Operator Meaning Explanation & Bitwise AND Multiplies the individual bits of operands Bitwise OR Adds the individual bits of operands Bitwise XOR Performs Exclusive OR operation Left shift Shifts the bits of the number towards left a specified number of positions Right shift Shifts the bits of the number towards right a specified number of positions and also preserves the sign bit. Zero fill right shift Shifts the bits of the number towards right a specified number of positions and it stores 0 (Zero) in the sign bit. Bitwise complement Gives the complement form of a given number by changing 0‟s as 1‟s and vice versa. · Ternary Operator or Conditional Operator (? :): This operator is called ternary because it acts on 3 variables. The syntax for this operator is: Variable = Expression1? Expression2: Expression3; First Expression1 is evaluated. If it is true, then Expression2 value is stored into variable otherwise Expression3 value is stored into the variable. e.g.: max = (ab) ? a: b; Program 1: Write a program to perform arithmetic operations //Addition of two numbers class AddTwoNumbers public static void mian(String args) int i=10, j=20; System.out.println("Addition of two numbers is : " + (i+j)); System.out.println("Subtraction of two numbers is : " + (i-j)); System.out.println("Multiplication of two numbers is : " + (ij)); System.out.println("Quotient after division is : " + (i/j) ); System.out.println("Remainder after division is : " +(i%j) ); Output: 10 Java Programming Program 2: Write a program to perform Bitwise operations //Bitwise Operations class Bits public static void main(String args) byte x,y; x=10; y=11; System.out.println ("x="+(x)); System.out.println ("x & y="+(x&y)); System.out.println ("x y="+(xy)); System.out.println ("x y="+(xy)); System.out.println ("x2="+(x2)); System.out.println ("x2="+(x2)); System.out.println ("x2="+(x2)); Output: 11 Java Programming 4. Control Statements Control statements are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution. In Java control statements are categorized into selection control statements, iteration control statements and jump control statements. · Java’s Selection Statements: Java supports two selection statements: if and switch. These statements allow us to control the flow of program execution based on condition. o if Statement: if statement performs a task depending on whether a condition is true or false. Syntax: if (condition) statement1; else statement2; Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value. The else clause is optional. Program 1: Write a program to find biggest of three numbers. //Biggest of three numbers class BiggestNo public static void main(String args) int a=5,b=7,c=6; if ( a b && ac) System.out.println ("a is big"); else if ( b c) System.out.println ("b is big"); else System.out.println ("c is big"); Output: o Switch Statement: When there are several options and we have to choose only one option from the available ones, we can use switch statement. Syntax: switch (expression) case value1: //statement sequence break; case value2: //statement sequence 12 Java Programming break; ………….….. case valueN: //statement sequence break; default: //default statement sequence Here, depending on the value of the expression, a particular corresponding case will be executed. Program 2: Write a program for using the switch statement to execute a particular task depending on color value. //To display a color name depending on color value class ColorDemo public static void main(String args) char color = „r‟; switch (color) case „r‟: System.out.println (“red”); break; case „g‟: System.out.println (“green”); break; case „b‟: System.out.println (“blue”); break; case „y‟: System.out.println (“yellow”); break; case „w‟: System.out.println (“white”); break; default: System.out.println (“No Color Selected”); Output: · Java’s Iteration Statements: Java‟s iteration statements are for, while and do-while. These statements are used to repeat same set of instructions specified number of times called loops. A loop repeatedly executes the same set of instructions until a termination condition is met. o while Loop: while loop repeats a group of statements as long as condition is true. Once the condition is false, the loop is terminated. In while loop, the condition is tested first; if it is true, then only the statements are executed. while loop is called as entry control loop. Syntax: while (condition) statements; 13 Java Programming Program 3: Write a program to generate numbers from 1 to 20. //Program to generate numbers from 1 to 20. class Natural public static void main(String args) int i=1; while (i = 20) System.out.print (i + “\t”); i++; Output: o do…while Loop: do…while loop repeats a group of statements as long as condition is true. In do...while loop, the statements are executed first and then the condition is tested. do…while loop is also called as exit control loop. Syntax: do statements; while (condition); Program 4: Write a program to generate numbers from 1 to 20. //Program to generate numbers from 1 to 20. class Natural public static void main(String args) int i=1; do System.out.print (i + “\t”); i++; while (i = 20); Output: 14 Java Programming o for Loop: The for loop is also same as do…while or while loop, but it is more compact syntactically. The for loop executes a group of statements as long as a condition is true. Syntax: for (expression1; expression2; expression3) statements; Here, expression1 is used to initialize the variables, expression2 is used for condition checking and expression3 is used for increment or decrement variable value. Program 5: Write a program to generate numbers from 1 to 20. //Program to generate numbers from 1 to 20. class Natural public static void main(String args) int i; for (i=1; i=20; i++) System.out.print (i + “\t”); Output: · Java’s Jump Statements: Java supports three jump statements: break, continue and return. These statements transfer control to another part of the program. o break:  break can be used inside a loop to come out of it.    break can be used inside the switch block to come out of the switch block.    break can be used in nested blocks to go to the end of a block. Nested blocks  represent a block written within another block. Syntax: break; (or) break label;//here label represents the name of the block. Program 6: Write a program to use break as a civilized form of goto. //using break as a civilized form of goto class BreakDemo public static void main (String args) boolean t = true; first: second: third: 15 Java Programming System.out.println (“Before the break”); if (t) break second; // break out of second block System.out.println (“This won‟t execute”); System.out.println (“This won‟t execute”); System.out.println (“This is after second block”); Output: o continue: This statement is useful to continue the next repetition of a loop/ iteration. When continue is executed, subsequent statements inside the loop are not executed. Syntax: continue; Program 7: Write a program to generate numbers from 1 to 20. //Program to generate numbers from 1 to 20. class Natural public static void main (String args) int i=1; while (true) System.out.print (i + “\t”); i++; if (i = 20 ) continue; else break; Output: 16 Java Programming o return statement:  return statement is useful to terminate a method and come back to the calling method.    return statement in main method terminates the application.    return statement can be used to return some value from a method to a calling method.  Syntax: return; (or) return value; // value may be of any type Program 8: Write a program to demonstrate return statement. //Demonstrate return class ReturnDemo public static void main(String args) boolean t = true; System.out.println (“Before the return”); if (t) return; System.out.println (“This won‟t execute”); Output: Note: goto statement is not available in java, because it leads to confusion and forms infinite loops. 17