6.61k likes | 6.64k Views
Introduction to Java. In 1990, Sun Microsystems (US) has 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
E N D
In 1990, Sun Microsystems (US) has 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 of 1991, Bill Joy, James Gosling, Mike Sheradin, Patrick Naughton, and several others met in Aspen, Colorado to disuss this project
Gosling thought C and C++ could be used to develop the project. But the problem he faced with them is that they were system dependent languages and hence could not be used on various processors, which the electronic device might use. So he started developing a new language, which was completely system independent. This language was initially called Oak. Since this name is registered by some other company, later it was changed to Java
Generally, Java is available in the form of j2sdk (version) J2sdk having the following components JVM (Java Virtual Machine) JRE (Java Runtime Environment) JDK (Java Development Kit) JIT (Just In time Compiler)
Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools. JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution time.
JDK 1.0 (January 23, 1996) Codename Oak. Initial release The first stable version was the JDK 1.0.2. is called Java 1 JDK 1.1 (February 19, 1997) J2SE 1.2 (December 8, 1998) Codename Playground J2SE 1.3 (May 8, 2000) Codename Kestrel. J2SE 1.4 (February 6, 2002) Codename Merlin.
J2SE 5.0 (September 30, 2004) Codename Tiger. Java SE 6 (December 11, 2006) Codename Mustang. Java SE 7 (July 28, 2011) Java 7 codename Dolphin Java SE 8(
Java Standard Edition (J2SE) • J2SE can be used to develop client-side standalone applications or applets. • Java Enterprise Edition (J2EE) • J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. • Java Micro Edition (J2ME). • J2ME can be used to develop applications for mobile devices such as cell phones.
Features of Java • Java is simple • Java is object-oriented • Java is distributed • Java is interpreted • Java is robust • Java is secure • Java is portable • Java’s performance • Java is multithreaded • Java is dynamic • System independence
Simple The concepts of pointers which is very difficult for both learners and programmers has been completely eliminated from java JavaSoft people maintained the same syntax of C and C++
Object-Oriented Java is object-oriented programming language. This means Java programs use objects and classes Distributed Using java, we can write programs, which capture information and distribute it to the clients Interpreted In Java, we use both compiler and interpreter for the execution
Robust Robust means strong. Java programs are strong and they don’t crash easily like C or C++. The reasons are in built exception handling mechanism and memory management features Secure Security problems like tampering and virus threats can be eliminated or minimized by using Java on Internet
Portability If a program yields the same result on every machine, then that program is called portable. Java programs are portable Performance The problem with interpreter inside the JVM is that it is slow. Because of this, Java programs used to run slow. To overcome this problem, along with the interpreter, JavaSoft people have introduced JIT (Just In Time) compiler, which enhances the speed of execution
Multithreaded A thread represents an individual process to execute a group of statements. JVM uses several threads to execute different blocks of code. Creating multiple threads is called ‘multithreaded’ Dynamic Before development of Java, only static text used to be displayed in the browser. Java applet program, which is dynamically interacting programs on Internet
System Independence Java’s byte code is not machine dependent. It can be run on any machine with any processor and any operating system
Hello World Program import java.lang.System; import java.lang.String; publicclass HelloWorld { public static void main(String[] args) { System.out.println(“Hello World!”); } }
import java.lang.System; import java.lang.String; Whenever we want to import several classes of the same package, we need not write several import statements, as shown above; instead, we can write a single statement as: import java.lang.*; Here, * means all the classes and interfaces of that package
public class HelloWorld After importing classes into the program, the next step is to write a class. Since Java is purely an object-oriented programming language, we cannot write a Java program without having at least one class. So, it is mandatory that every Java program should have at least one class in it Writing a Class We should use class keyword for this purpose and then write the class name A class code starts with a { and ends with a }
public static void main (String args[]) A method generally, performs two operations • It can accept some data from outside • It can also return some result Example: sqrt() Similarly the main() method also accept some data from us Example: it accepts a group of string, which is called a string type array. Here args[] is the array name and it is of String type. The values passed to main() are called arguments
If a method is not menat to return any value, then we should write void before that method’s name. main() method does not return any value, so void should be written before that method’s name We should call the main() method without creating an object. Such methods are called static methods and should be declared as static JVM is a program written by JavaSoft people and main() is the method written by us. Since, main() method should be available to the JAM, it should be declared as public
System.out.println(“Hello World”); System.out gives the PrintStream class object. This object, by default, represents the standard output device, i.e. the monitor. So, the string “Hello World” will be sent to the monitor
Identifiers Identifiers • A name in the program is an identifier it may be class name or method name, variable name or label name. Example Class Test { public static void main(String[] args) { int x=10 System.out.println(x); }
Rules for defining Identifiers • Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). • Identifiers cannot start with a number • After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers. • In practice, there is no limit to the number of characters an identifier can contain. • You can't use a Java keyword as an identifier. • Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.
Legal Identifiers • int _a; • int $c; • int ______2_w; • int _$; • int this_is_a_very_detailed_name_for_an_identifier; Illegal Identifiers • int :b; • int -d; • int e#; • int .f; • int 7g;
Java coding Standards Coding standards for classes Usually class name should be noun. Should starts with upper case letter and if it contain multiple words every inner words also should start with capital letters. Example: • String • StringBuffer • NumberFormat • CustomerInformation
Coding standards for Interfaces Usually interface named should be adjective, starts with capital letters and if it contains multiple words, every inner word also should starts with capital letter. Example: • Runnable • Serializable • Clonable • Movable • Transferable • Workable
Coding standards with methods • Values should be either verbs or verb + noun combination. • Starts with lower case and every inner words starts with upper case(this convention is also called camel case convention). Example: • getName(), getMessage(), toString(), show(), display(). Coding standards for variables • Usually the variable starts with noun and every inner word should start with upper case i.e camel case convention. Example: • Name, rollno, bandwidth, totalNumber. Coding standards for constants • It should be noun, it should contain only upper case letters and works are separated with underscores. • Example: • MAX_SIZE, MIN_PRIORITY, COLLEGE_NAME.
Primitive Data Types Represent numbers, characters, boolean values • Integers: byte, short, int, and long • Real numbers: float and double • Characters: char • Boolean :boolean
Except boolean and char all the remaining datatypes are signed datatypes i.e we can represent both +ve and –ve numbers. byte • Size : 8-bits • Range: -128 to 127 Example: • byte b = 10; • byte b = 127; • byte b = 130; C.E: possible loss of precision • byte b = true; C.E: Incompatible types found: boolean required: byte byte datatype is best suitable if we are handling data either from file or form network.
short • size = 2 bytes • range = -215 to 215 -1 (-32768 to 32767) Example: • short s = 10; • short s = 32767; • short s = 65535; C.E: possible loss of precision. • short s = true; C.E: Incompatible types short is best suitable datatype for 16-bit process. But currently these are completely out dated and hence the corresponding datatypes also no one is using. int The most commonly used datatype is int. • size = 4 bytes • range = -231 to 231 – 1(-2147483648 to 2147483747) The size of int is always fixed irrespective of platform hence the chance of failing java program is very less if you are changing the platform hence the java is considered as Robust.
long if int is not enough to hold big values then we should go for long-datatype • size = 8 bytes • range = -263 to 263 – 1 Example: • The amount of distance traveled by light in 1000days can be represented by long datatype only and int is not enough.
floating-point For representing real numbers(numbers with decimal points) float • size = 4 bytes • range = -3.4e38 to 3.4e38 • If 6 to 7 decimal places of accuracy required double • size = 8 bytes • range = -1.7e308 to 1.7e308 • If 14 to 15 decimal places of accuracy required
boolean datatye • size = not applicable(virtual machine dependent). • range = not applicable but allowed values are true/false. Which of the following boolean declarations are valid • boolean b1 = true; • boolean b2 = 0; Incompatible types found:int required : booleaan • boolean b3 = TRUE; capital TRUE is not valid.
char Data Type • One Unicode character (16 bits - 2 bytes) TypeSize Minimum Value Maximum Value in Bytes char 2 character character encoded as 0 encoded as FFFF Example declarations: char finalGrade; char newline, tab, doubleQuotes;
Literal Values for All Primitive Types A primitive literal is merely a source code representation of the primitive data types you type in while writing code. The following are examples of primitive literals: • 'b' // char literal • 42 // int literal • false // boolean literal • 2546789.343 // double literal
Integer Literals • There are three ways to represent integer numbers in the Java language: decimal (base 10), octal (base 8), and hexadecimal (base 16). Decimal Literals • Decimal integers need no explanation; you've been using them since grade one or earlier. • In the Java language, they are represented as is, with no prefix of any kind, as follows: int length = 343;
Octal Literals • Octal integers use only the digits 0 to 7. • In Java, you represent an integer in octal form by placing a zero in front of the number, as follows: class Octal { public static void main(String [] args) { int six = 06; // Equal to decimal 6 int seven = 07; // Equal to decimal 7 int eight = 010; // Equal to decimal 8 int nine = 011; // Equal to decimal 9 System.out.println("Octal 010 = " + eight); } } • Notice that when we get past seven and are out of digits to use we revert back to zero, and one is added to the beginning of the number.
Hexadecimal Literals • Hexadecimal (hex for short) numbers are constructed using 16 distinct symbols. • Because we never made-up single digit symbols for the numbers 10 through 15, we use alphabetic characters to represent these digits. • Counting from 0 through 15 in hex looks like this: • 0 1 2 3 4 5 6 7 8 9 a b c d e f • Java will accept capital or lowercase letters for the extra digits • You are allowed up to 16 digits in a hexadecimal number, not including the prefix 0x • All of the following hexadecimal assignments are legal: class HexTest { public static void main (String [] args) { int x = 0X0001; int y = 0x10 int z = 0x11 System.out.println("x = " + x + " y = " + y + " z = " + z); } }
Floating-Point Literals • Floating-point numbers are defined as a number, a decimal symbol, and more numbers representing the fraction. • double d = 11301874.9881024; • In the preceding example, the number 11301874.9881024 is the literal value. • Floating-point literals are defined as double (64 bits) by default, so if you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the suffix F or f to the number. • If you don't, the compiler will complain about a possible loss of precision, because you're trying to fit a number into a (potentially) less precise "container." float f = 23.467890; // Compiler error, possible loss of precision float g = 49837849.029847F; // OK; has the suffix "F“ • You may also optionally attach a D or d to double literals, but it is not necessary because this is the default behavior. • double d = 110599.995011D; // Optional, not required • double g = 987.897; // No 'D' suffix, but OK because the literal is a double by default • Look for numeric literals that include a comma, for example, • int x = 25,343; // Won't compile because of the comma
Boolean Literals • Boolean literals are the source code representation for boolean values. • A boolean value can only be defined as true or false. boolean t = true; // Legal boolean f = 0; // Compiler error! • Be on the lookout for questions that use numbers where booleans are required. • You might see an if test that uses a number, as in the following: int x = 1; if (x) { } // Compiler error!
Character Literals • A char literal is represented by a single character in single quotes. char a = 'a'; char b = '@'; • You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with \u as follows: char letterN = '\u004E'; // The letter 'N' • Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). • For example, the following are all legal: char a = 0x892; // hexadecimal literal char b = 982; // int literal char c = (char)70000; // The cast is required; 70000 is out of char range
Literal Values for Strings • A string literal is a source code representation of a value of a String object. • For example, the following is an example of two ways to represent a string literal: String s = "Bill Joy"; System.out.println("Bill" + " Joy"); • Although strings are not primitives, they're included in this section because they can be represented as literals—in other words, typed directly into code. • The only other nonprimitive type that has a literal representation is an array