





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of Java programming, including its features such as simplicity, security, and portability. It also covers the Java environment and the process of building and running Java applications. Java is a popular programming language known for its ability to create cross-platform applications, and this document serves as a useful resource for both beginners and experienced programmers.
Typology: Assignments
1 / 29
This page cannot be seen from the preview
Don't miss anything!
Sun Microsystems released the first version 1.0 of java in 1995. It was followed by java 1.02 two months later. In this version database connectivity and distributed object features are added. Version 1.1 released in 1997, added a robust event model, awt enhancements, jar files, RMI (Remote Method Invocation) and Beans. Version 1.2 released at the end of 1998, has the feature of Swing, Java 2D, Accessibility Drag and Drop, JAVA IDL (Java Interface Definition Language), JNI (Java Native Interface) enhancements and JVMDI (Java Virtual Machine Debug Interface). JDK 1.2 is called Java 2 platform. Present version of java is JAVA SE 10.
Java Applets: An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser.
Security: Users are worried about their systems with viral infection and malicious programs. Java answers these issues by providing a “firewall” between a networked application and your computer.
Portability: Many types of computers and operating systems are in use throughout the world - and many are connected to the Internet. For programs to be dynamically downloaded to all platforms connected to the Internet, some means of generating portable executable code is needed. Java provides portability.
hierarchical classification. This is possible by deriving a new class called derived class from an existing class called base class. The new class will have the combined features of both the classes. Polymorphism : Polymorphism is another important OOP concept. Polymorphism means the ability of giving the same name to different methods. The individual methods may implement different tasks but can be differentiated by the type of arguments passed to them.
JDK: The JDK comes with a collection of tools that are used for developing and running java programs. JDK includes: appletviewer – Enable us to run java applets with actually using java compatible browser. javac – The java compiler, which translates java source code to bytecode file that the interpreter can understand.
java – java interpreter interpret the bytecode and run the instruction. javap – Java disassemble, which enables us to convert bytecode file into a program description. Rmic – create skeletons and stubs for remote method invocation. javah – Produces header files for use with native methods. javadoc – creates HTML format documentation from java source code files. jdb – Java debugger, which helps us to find errors in our programs.
JSL or API: Java Standard Library (JSL) or Application Programming Interface (API) includes hundreds of classes and methods grouped into several functional packages. JDK 1.2 has 58 packages. The most commonly used packages are:
java.lang – It contains the main language support class deals with Object wrappers, Strings, Multithreading and basic features of java. java.util – It provides classes that support collections, date, time and calendar operations, parsing and basic event processing. java.io – This package provides reading and writing data in the form of streams and device independent file services. java.awt – This package provides classes for creating GUI programs. It contains, java’s Abstract Windowing Toolkit (AWT) etc. java.applet – This package includes a set of classes with support for HTML embedded java applets. java.net – It combines the classes supporting low-level internet programming and www. javax.swing – It offers classes for components, higher level controls and pluggable look and feel.
programmer would like to refer to at a later stage. Comments must explain why and what of classes and how of algorithms. The comment lines must be in between /** and */.
Package statement: The first statement allowed in a java is a package statement. This statement declares a package name and informs the compiler that the classes defined here belong to this package. For example: package colleges;
Import Statements: This section contains all import packages. These statements instruct the interpreter to load the respected classes from the packages. Example: import java.util.*;
Interface statements: An interface is like a class but includes a group of method declarations. This is an optional section and is used only when we wish to implement inheritance feature in the program.
Class Definitions: A java program may contain multiple class definitions. Classes are the primary and essential elements of a java program. These classes are used to map the objects of real-world problems. The number of classes used depends on the complexity of the problem.
Main method class: Since every java standard-alone program requires a main method as its starting point, this class is the essential part of a java program. The main method creates objects of various classes and establishes communications between them. On reaching the end of main, the program terminates and the control passes back to the operating system.
Keywords: keywords are essential part of a language definition. They implement specific feature of the language. Java has reserved 50 words as keywords. Understanding of all these words is important for java programs. Examples: class, import, double, for etc.
Identifiers: Identifiers are programmer designed tokens. They are used for naming classes, methods, variables, objects etc. Java identifiers follow the following rules. a) They can have alphabets, digits and the underscore and dollar sign characters. b) They must not begin with a digit. c) Uppercase and lowercase letters are distinct. d) They can be of any length.
Literals: Literals in java are a sequence of characters that represent constant values to be stored in variables. Java specifies 5 types of literals. They are: Inters literals, float point literals, character literals, string literals and Boolean literals.
Operators and separators: An operator is a symbol that takes one or more arguments and operates on them to produce a result. Operators are of many types: Arithmetic, Relational, Logical, Assignment, Increment etc. Separators are symbols used to indicate groups. For example, ( ), {},[ ] etc.
Integers : Java defines four integer types: byte, short, int and long. All these are signed values. Java does not support the concept of unsigned integers. The width and ranges of these integers vary widely, as shown in this table.
Characters : In java, the data type used to store characters is char. Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. For this purpose, it requires 16 bits. Thus, in java char is a 16 bit type. The range of a char is 0 to 65,536. There are no – ve chars.
Boolean : For logical values, java has a primitive type called boolean. It can have only one of two possible values, true or false. This is the type returned by all conditional expressions that govern the control statements. For example the declaration of variable b of type Boolean is: boolean b;
type identifier [= value] [, identifier [= value]... ];
Example: int a=7, b=56; float ave; int i;
Java allows variables to be initialized dynamically, using any valid expression at time the variable is declared. For example, double c = Math.sqrt(aa + bb);
Java allows variables to be declared within any block. A block defines a scope. A scope determines what objects are visible to other parts of your program. It also determines the life time of those objects. Variables are created when their scope is entered and destroyed when their scope is left. This means that a variable will not hold its value once it has gone out of scope. Thus the life time of a variable is confined to its scope.
ii. Explicit type casting: To create a conversion between two incompatible types, you must use a cast. A cast is simply an explicit type conversion. For example, what if you want to assign an int value to byte variable? This conversion will not be performed automatically, because a byte is smaller than an int. This kind of conversion is called a narrowing conversion. Example: int a=5; byte b = (byte) a; Here the value of a is explicitly converted into byte type before storing into the variable b.
Type casting Rules a. A Boolean variable cannot be type cast. b. If the range is narrowed, an explicit type cast is required. c. If the conversion increases the range of data that can be store, it is some time known as promotion or widening. d. If any of the operand type is double, the other one will be converted to double. This works similarly long, float, int, short, and byte. e. Java automatically promotes byte or short operand to int.
Two Dimensional Array (or) Multidimensional Array: In java, multidimensional arrays are actually arrays of arrays. Java allows us to define tables of values by using two dimensional arrays. For example, the following declares a two dimensional array variable called twod with 4 rows and 5 columns. int twod[ ][ ] = new int[4][5]; (or) int twod[ ][ ] = new int [4] [ ]; twod[0] = new int [5]; twod[1] = new int [5]; twod[2] = new int [5]; twod[3] = new int [5];
Two dimensional arrays can be initialized during declarations. By default all memory locations are initialized with zeros. We can do it in one of two ways. For example, twod is a 2 by 3 two dimensional array initialized in two ways as follow:
int twod[2 ] [3 ] = {12, 23, 9, 45, 22, -9}; // Initializing with specific values.
int twod[2 ] [3 ] ={{12, 23, 9},{ 45, 22, -9}}; // Initializing with same values.
Two Dimensional array with variable sizes int twod[ ][ ] = new int [4] [ ]; twod[0] = new int [1]; twod[1] = new int [2]; twod[2] = new int [3]; twod[3] = new int [4];
Arithmetic Operators : Arithmetic operators are used in mathematical expressions in the same way as used in algebra. The operands of the arithmetic operators must be of a numeric type. The following table lists the arithmetic operators. Lets a, b and c are integer type operands.
Operator Description Example
Bitwise Operators : Java defines several bitwise operators which can be applied to the integer types: long, int, short, char and byte. These operators act upon the individual bits of their operands. They are summarized in the following table.
Operator Description Example ~ Bitwise Unary NOT c = ~ a & Bitwise AND c = a & b | Bitwise OR^ c = a| b ^ Bitwise exclusive OR c = a ^ b
Shift right c = a >> 2 << Shift left c = a << 2 &= Bitwise AND assignment a&=b (or) a = a & b | = Bitwise OR assignment a | =b (or) a = a | b ^= Bitwise exclusive ORassignment A^=b (or) a = a ^ b = Shift right assignment a>>=b (or) a =a>>b <<= Shift left assignment A<<=b (or) a =a<<b
Selection: Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Java support two selection statements: if and switch. i. If – The if statement is java’s conditional branch statement. It can be used to route the program execution through two different paths. It is also called two-way selection statement. The general form are: a) If (condition) {statement1; statement2;
....... .. ;} else {statement1; statement2; ....... .. ;} b) Nested ifs – A nested if is an if statement within another if or else. Nested ifs are very common in programming. Example, if (i==10) { if (j < 20) a=b; if (K>100) c= d; else a=c; } else a = d;
c) The if–else–if Ladder: A sequence of nested ifs is the if-else-if ladder. It looks like this: if (condition) statement1; else if (condition) statement2; else if (condition) statement3;
......... else { statement n;}
ii. Switch – The switch statement is a multi-way selection statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. The general form of a switch statement: Switch ( expression) { case value1: sequence of statements separated by semicolon break; case value2: sequence of statements separated by semicolon break;
... .............. Case value n: sequence of statements separated by semicolon break; default: sequence of statements separated by semicolon if any
}
Exmple:
switch (i) { case 1: System.out.print(“January”); break; case 2: System.out.print(“February”); break; case 3: System.out.print(“March”); break; case 4: System.out.print(“April”); break; case 5: System.out.print(“May”); break; case 6: System.out.print(“June”); break; case 7: System.out.print(“July”); break; case 8: System.out.print(“August”); break case 9: System.out.print(“September”);
executed once again. This process is repeated until the test condition value is false. On exit, the program continues with the statement immediately after the body of the loop.
Example: Whiel (i < 10) { i++; System.out.print(“While statement”); }
do .. while – The do while is an exit-controlled loop statement. The control executes the body of the loop and tests the condition at the bottom of the loop, if it is true then the body of the loop is executed another time. This process is repeated until the test condition value is false. On exit, the program continues with the statement immediately after the body of the loop. The body of the loop is always executed at least once.
Example: do { i++; System.out.print(“While statement”); } Whiel (i < 10)
break – An early exit from a loop can be accomplished by using the break statement. When the break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loops are nested, the break will only exit from the loop containing it. That is, the break will exit only a single loop.
continue – java support another jump statement called continue statement. Unlike the break which causes the loop to be terminated, the continue causes the loop to be continued with the next iteration after skipping any statements in between. The format of the continue statement is simply continue;.
return – The return statement is used to explicitly return from a method. It causes program control to transfer back to the caller of the method. The general form of return statement is: return;.
Highest Precedence ( ) [ ]. ++ -- ~!
<< = < <= = = != & ^ | && || ?: = op= Lowest Precedence