Core Java introduction | Basics | free course

56
Session 1 - Introduction to Java

Transcript of Core Java introduction | Basics | free course

PowerPoint Presentation

Session 1 - Introduction to Java

KernelTraining.com

What Is Java?JavaJava is not just a programming language but it is a complete platform for object oriented programming.JREJava standard class libraries which provide Application Programming Interface and JVM together form JRE (Java Runtime Environment).JDK JDK (Java development kit) provides all the needed support for software development in Java.

KernelTraining.com/core-java

KernelTraining.com

Java Virtual Machine (JVM)Runs the Byte Code.Makes Java platform independent.Handles Memory Management.

KernelTraining.com/core-java

How Java works ?

KernelTraining.com/core-java

How Java works ?Java compilers convert your code from human readable to something called byte code in the Java world.Byte code is interpreted by a JVM, which operates much like a physical CPU to actually execute the compiled code.Just-in-time (JIT) compiler is a program that turns Java byte code into instructions that can be sent directly to the processor.

KernelTraining.com/core-java

HistoryJames Gosling and Sun MicrosystemsOakJava, May 20, 1995, Sun WorldHot Java The first Java-enabled Web browserJDK EvolutionsJ2SE, J2ME, and J2EE KernelTraining.com/core-java

Characteristics of JavaPlatform IndependentPortableObject OrientedRobust & SecureDistributedSimple & SmallMulti ThreadedDynamic Compile & InterpretedHigh PerformanceKernelTraining.com/core-java

JDK VersionsJDK Alpha and Beta (1995)JDK 1.0 (January 23, 1996)JDK 1.1 (February 19, 1997)J2SE 1.2 (December 8, 1998)J2SE 1.3 (May 8, 2000)J2SE 1.4 (February 6, 2002)J2SE 5.0 (September 30, 2004)Java SE 6 (December 11, 2006) Java SE 7 (July 28, 2011)Java SE 8 (March 18, 2014) Java SE 9Java SE 10KernelTraining.com/core-java

JDK EditionsJava 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 Server Pages. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such as cell phones. KernelTraining.com/core-java

Java IDE ToolsForte by Sun Microsystems Borland JBuilderMicrosoft Visual J++WebGain Caf IBM Visual Age for Java KernelTraining.com/core-java

Getting Started with Java ProgrammingA Simple Java ApplicationCompiling ProgramsExecuting Applications

KernelTraining.com/core-java

A Simple ApplicationExample 1.1//This application program prints Welcome//to Java!

public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}

RunSourceNOTE: To run the program, install slide files on hard disk.KernelTraining.com/core-java

Creating and Compiling ProgramsOn command linejavac file.java

KernelTraining.com/core-java

Examplejavac Welcome.java

java Welcome

output:...

KernelTraining.com/core-java

CommentsIn Java, comments are preceded by two slashes (//) in a line, or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */. KernelTraining.com/core-java

Java Basic Data Types

KernelTraining.com/core-java

Program demonstrating DataTypesclass Second { public static void main (String args[ ]) { int x - 90; short y = 4; float z = 10.87f; String name = "Sarti"; System.Out.println( "The integer value is " + x) ; System.out.println ("The short value is " + y); System.out.println ("The float value is " + z); System.out.println ("The string value is " + name); } }

KernelTraining.com/core-java

Save this file as Second. java and compile using javac Second.java at DOS prompt. On successful compilation, execute the program using Java Second The output is displayed as : The integer value is 90 The short value is 4 The float value is 10.87 The string value is SartiThe lines 3-6 depict the declaration, initialization and naming of various data types. The values of the declared variables are printed from lines 7-10. The + operator is used here as a concatenation operator .

KernelTraining.com/core-java

OperatorsOperators are special symbols used in expressions. They include arithmetic operators, assignment operators, increment and decrement operators, logical operators, bitwise operators and comparison operators.

KernelTraining.com/core-java

Arithmetic OperatorsJava has five basic arithmetic operators. Each of these operators takes two operands, one on either side of the operator. The list of arithmetic operators is given below:

OperatorMeaningExample+Addition8+10-Subtraction10-8*Multiplication20*84/Division10/5%Modulus10% 6

KernelTraining.com/core-java

Example illustrates the usage of various arithmetic operators.class Three {public static void main (String args[ ]) { int x = 10; int y = 20; float z = 25.98f; System.out.println ("The value of x + y is " + (x + y)); System. out. println ("The value of z - y is " + (z - y) ); System.out.println ("The value of x * y is " + (x * y)); System.out.println ("The value of z / y is " + (z / y)); System.out.println ("The value of z % y is " + (z % y)); } }

KernelTraining.com/core-java

Save this file as Three.java and compile using javac Three.java at DOS prompt. On successful compilation, execute the source code using: Java ThreeThe output appears as shown below:The value of x + y is 30The value of z - y is 5.98The value of x * y is 200The value of z / y is 1.299The value of z % y is 5.98Notice the usage of the various arithmetic operators from lines 6-10.KernelTraining.com/core-java

Assignment OperatorsThe assignment operators used in C and C++ are also used in Java. A selection of assignment operators is given.ExpressionMeaningx += yx = x + yx -+ yx = x-yx *= yx = x * yx /= yx = x /yx=yx=y

KernelTraining.com/core-java

Example demonstrates the various operator assignments in action.class assign{ public static void main(String args[ ]) { int a=1; int b=2; int c=3; a+=5; b*=4; c+=a*b; c%=6; System.out.println ("a=" +a); System.out.println("b=" +b); System.out.println("c=" +c); } }KernelTraining.com/core-java

Save this file as assign.java and compile using javac assign.java at DOS prompt. On successful compilation, execute the source code using: Java assign The output appears as shown below:a=6b=8c=3KernelTraining.com/core-java

Incrementing and DecrementingTo increment or decrement a value by one, the ++ operator and operator are used respectively. The increment and the decrement operators can be prefixed or post fixed. The different increment and decrement operators can be used as given below: ++a (Pre increment operator) -Increment a by 1, then use the new value of a in the expression in which a resides. a++ (Post increment operator) - Use the current value of a in the expression in which a resides and then increment a by 1. - -b (Pre decrement operator) - Decrement b by 1, then use the new value of b in the expression in which b resides. b- -(Post decrement operator) - Use the current value of b in the expression in which b resides and then decrement b by 1.

KernelTraining.com/core-java

Example demonstrates the usage of the increment and decrement operators.class IncDec { public static void main (String args[ ] ) { int a =1; int b=2; int c=++b; int d=a++; C++; System.out.println ("a =" +a); System.out.println ("b =" +b); System.out.println ("c =" +c); System.out.println ("d =" +d) ; }}

KernelTraining.com/core-java

Save this file as IncDec.java and compile using javac IncDec.java at DOS prompt. On successful compilation, execute the source code using : Java IncDec The output appears as given below:a=2b=3c=4d=lKernelTraining.com/core-java

Comparison OperatorsThere are several expressions for testing equality and magnitude. All these expressions return a Boolean value. Table 1.5 enlists the comparison operators.

OperatorMeaningExample==Equalu==45! =Not equalu!=7568= 64

KernelTraining.com/core-java

Logical OperatorsLogical operators available are AND, OR, XOR and NOT.The AND operator (& or &&) returns true only if both sides in an expression are true. If any one side fails, the operator returns false. For example, consider the following statementgender ==1 && age >=65This condition is true if and only if both the simple conditions are true. In the && operator, if the left side of the expression returns false, the whole expression returns false and the right side is totally neglected. The & operator evaluates both sides irrespective of outcome.KernelTraining.com/core-java

The I or II is used for OR combination. This returns true if any of the expressions is true. Only if all the conditions are false, the expression is false.Consider the following statementsemesterAverage>=90 | | finalExam >=90The above condition is true if either of the two conditions is true. The II evaluates the left expression first and if it returns true, never evaluates the right side expression. A single I evaluates both the expressions regardless of the outcome.The XOR operator indicated by ^, returns true only if its operands are different. If both its operands are similar (true-true, false-false), it returns false.The ! operator is used for NOT. The value of NOT is negation of the expression.KernelTraining.com/core-java

Bitwise OperatorsThe bitwise operators are inherited from C and C++. They are used to perform operations on individual bits in integers. A list of the bitwise operators available is given OperatorMeaning&Bitwise ANDIBitwise OR^Bitwise XOR>Right Shift>>>>Zero fill right shift~Bitwise Complement=Right Shift assignment>>>=Zero fill right shift assignmentx&=yAND assignmentx l=yOR assignmentx ^= yXOR assignment

KernelTraining.com/core-java

The following example demonstrates the usage of bitwise operators.class BitOp { public static void main (String args [ ]) { int a=1; int b=2; int c=3; a=a|4; b>>=1; c