Module - 2 Overview of Java History of Java Technology?

Size: px
Start display at page:

Download "Module - 2 Overview of Java History of Java Technology?"

Transcription

1 Overview of Java Module - 2 Java is an object-oriented programming language with its own runtime environment. Java is a programming language and a platform. Java is a high level, robust, secured and object-oriented programming language. Platform: Any hardware or software environment in which a program runs is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform. Java was developed by Sun Microsystems Inc. in 1991, later acquired by Oracle Corporation. It was conceived by James Gosling and Patrick Naughton. It took 18 months to develop the first working version. (The initial name was Oak but it was renamed to Java in 1995 as OAK was a registered trademark of another Tech company). Java programs are platform independent which means they can be run on any operating system with any type of processor as long as the Java interpreter is available on that system. A Java interpreter executes lines of byte code as commands to be executed. The byte code is executed. The JVM takes the byte code and generates machine code. The byte code is compiled to machine code, and the machine code is executed. Java code that runs on one platform does not need to be recompiled to run on another platform, it s called Write Once, Run Anywhere (WORA). Java Virtual Machine (JVM) executes Java code, but is written in platform specific languages such as C/C++/ASM etc. JVM is not written in Java and hence cannot be platform independent and Java interpreter is actually a part of JVM. The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. For example: J2EE for Enterprise Applications, J2ME for Mobile Applications. History of Java Technology? History of Java programming language is usually associated with origin predates the web. James Gosling, Patrick Naughton, Chris Warth, Mike Sheridan and Ed Frank initiated the Java language project in June The idea was to Jagadeesh A Y, Dept. of CSE SPCE 1

2 develop a language which was platform-independent and which could create embedded software for consumer electronic devices. The language took 18 months to develop and had an initial name as Oak which was renamed to Java in 1995, due to copyright issues. Originally, developed by James Gosling at Sun Microsystems (which has since merge into Oracle Corporation) and released in JDK 1.0 released In January 23, Where is Java being used? Earlier, Java was only used to design and program small computing devices but later adopted as one of the platform independent programming language and now according to Sun, 3 billion devices run Java. JSP Java is used to create web applications like PHP and ASP, JSP (Java Server Pages) used with normal HTML tags, which helps to create dynamic web pages. Applets This is another type of Java program that used within a web page to add many new features to a web browser. J2EE The software Java 2 Enterprise Edition are used by various companies to transfer data based on XML structured documents between one another. JavaBeans This is something Visual Basic, a reusable software component that can be easily assemble to create some new and advances application. Mobile Besides the above technology, Java is also used in mobile devices to build various kinds of applications. Types of Java Applications Web Application: Java is used to create server-side web applications. Currently, servlets, jsp, struts, jsf etc. technologies are used. Standalone Application: It is also known as desktop application or window-based application. An application that need to be installed on every machine or server such as media player, antivirus etc. AWT and Swing are used in Java for creating standalone application. Enterprise Application: An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications. Mobile Application: Java is used to create application for mobile devices. Currently Java ME is used for creating applications for small devices. Jagadeesh A Y, Dept. of CSE SPCE 2

3 Features of Java Object Oriented In java everything is an Object. Object oriented programming is a way of organizing programs as collection of objects, each of which represents an instance of a class. Platform independent During compilation, the compiler converts java program to its byte code. This byte code can run on any platform such as Windows, Linux, Mac / OS etc. which mean a program that is compiled on windows can run on Linux and vice-versa. This is why java is known as platform independent language. Easy to learn Java is simple because, syntax is based on C or C++. Secure It provides a virtual firewall between the application and the computer. Java codes are confined within Java Runtime Environment (JRE). Thus, it does not grant unauthorized access on the system resources. Architectural-neutral Java compiler generates an architectureneutral object file format which makes the compiled code to be executable on many processors, with the presence of java runtime system. Portable Java code that is written on one machine can run on another machine. The platform independent byte code can be carried to any platform for execution that makes java code portable. Multi-threaded With Java s multi-threaded feature it is enabled a program to perform several tasks simultaneously. High Performance With the use of Just-In-Time compilers Java enables high performance. Jagadeesh A Y, Dept. of CSE SPCE 3

4 Java Editors To write java programs, you will need a text editor. There are even more sophisticated IDE available. Notepad On Windows machine you can use any simple text editor like Notepad, TextPad. Netbeans is a Java IDE that is open source and free which can be downloaded from Eclipse A Java IDE developed by the open-source community and can be downloaded from Environment Setup and Java Program Structure Before Installation and setting up of environment variable, let us know about Java SE Development Kit (JDK). It contains 1. JDK (Java Development Kit) 2. JRE (Java Run Time Environment) 3. JVM (Java Virtual Machine) Java Development Kit (JDK) JDK contains everything that will be required to develop and run Java application. JDK includes a complete JRE (Java Runtime Environment) plus tools for Developing, Debugging, and Monitoring Java applications. JDK is needed to develop Java applications and Applets as well as run them. It contains o Java Run time Environment (JRE) o Interpreter / Loader (java) o Compiler (javac) o Archiver (jar) o Documentation Generator (java doc) javac: javac is the compiler for the Java programming language, it s used to compile.java file. Example: c:\> javac TestFile.java java: command is used to compile a java program, it creates a class file which can be run by using java command. Example: c:\> java TestFile.class Jagadeesh A Y, Dept. of CSE SPCE 4

5 javadoc: JavaDoc is a API documentation generator for the Java language, which generates documentation in HTML format from Java source code. appletviewer: Applet viewer run and debug applets without a web browser, its standalone command-line program to run Java applets. jar: A jar is (manage Java archive) a package file format that contains class, text, images and sound files for a Java application or applet gathered into a single compressed file. Java Runtime Environment JRE contains everything required to run Java application which has already been compiled. It doesn t contain the code library required to develop Java application. Java Virtual Machine (JVM) The Java Virtual Machine is called JVM, is an abstract computing machine or virtual machine interface that drives the java code. Mostly in other Programming Languages, compiler produce code for a particular system but Java compiler produces Bytecode for a JVM. Bytecode is an intermediary language between Java source and the host system. JVM interprets the bytecode to OS specific instructions. JVM is platform independent. JVM is responsible to allocate the necessary memory needed by the java program. JVM is responsible to de allocate a memory. The JVM is the heart of the Java language s developed based on "write-once, runanywhere" principle. JRE = JVM + Required Library to run Application. JDK = JRE + Required Library to develop Java Application. Jagadeesh A Y, Dept. of CSE SPCE 5

6 First Program in Java Let us create HelloWorld.java program, class HelloWorld public static void main(string args[]) System.out.println("HelloWorld!!!"); In the above program, class is a keyword used to declare a class in java. public is a keyword used to specify access modifier. Public means it is visible to all. static is a keyword, if we declare any method as static, it is known as static method. The advantage of static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. void is the return type of the method, it means it doesn't return any value. main represents beginning of the program. String[] args is used for command line argument. System.out.println() is used print statement. Note: Name of the file & name of a class must be same. How to Compile and Execute java program To Compile: javac helloworld.java To Execute: java helloworld Jagadeesh A Y, Dept. of CSE SPCE 6

7 Data types and other tokens Data type: It specifies type of data to be stored in variables. Whereas variables are the names given to memory locations in which values can be stored. There are two different data types in Java, namely Primitive Data type Reference / Object Data type Primitive Data Type By default, primitive data types are supported by Java. Each type is represented using keyword. There are eight different primitive data types, namely byte short int long float double char boolean The following should be understood for every data type: 1. Memory size allocated. 2. Default value. 3. Range of values it can represent. 4. Minimum values can be represented. 5. Maximum value can be represented. Boolean 1. Allocated Memory Size: 1 bit 2. Default value: False 3. Keyword: boolean 4. Example: boolean a = TRUE; 5. It represents either TRUE or FALSE Jagadeesh A Y, Dept. of CSE SPCE 7

8 Byte Allocated Memory Size: 1 byte or 8 bits Default value: Zero Keyword: byte Example: byte a = 100 Minimum Value can be represented: -128 (-27) Maximum Value can be represented: 127 Char Allocated Memory Size: 2 bytes or 16 bits Default value: '\u0000' Keyword: char Example: char ch = 'A'; Minimum Value can be represented: '\u0000' (0) Maximum Value can be represented: '\uffff' (65,535) Short Allocated Memory Size: 2 bytes or 16 bits Int Default value: Zero Keyword: short Example: short s = 1000; Minimum Value can be represented: -32,768 (215) Maximum Value can be represented: 32,767 (215-1) Allocated Memory Size: 4 bytes or 32 bits Default value: Zero Keyword: int Example: int a = 10; Minimum Value can be represented: - 2,147,483,648 (-231) Maximum Value can be represented: 2,147,483,647 (231) Jagadeesh A Y, Dept. of CSE SPCE 8

9 Float Allocated Memory Size: 4 bytes or 32 bits Default value: 0.0f Keyword: float Example: float x = 2.3f; Minimum Value can be represented: 1.4E-45 Maximum Value can be represented: E38 Long Allocated Memory Size: 8 bytes or 64 bits Default value: 0L Keyword: long Example: long b = L; Minimum Value can be represented: Maximum Value can be represented: Double Allocated Memory Size: 8 bytes or 16 bits Default value: 0.0d Keyword: double Example: double d = Minimum Value can be represented: 4.9E-324 Maximum Value can be represented: E308 Tokens Tokens are group of characters combined to form meaningful word. There are five types of tokens, namely 1. Keywords 2. Identifiers 3. Literals 4. Operators 5. Separators Jagadeesh A Y, Dept. of CSE SPCE 9

10 Keywords Keywords are also called as reserved words. Keywords have predefined meaning in Java language. There are 49 keywords are currently available in Java language. abstract assert boolean break byte case catch char class const continue default do double else extends false final finally float for goto if implements import instanceof int interface long native new package private protected public return short static super switch synchronized this throw transient true try void volatile while Identifiers Identifiers are the names given for variables, classes, functions, objects, packages, interfaces in Java program. Rules to write variables 1. It may consists of letters, digits or underscore ( _ ). 2. First character must be either letter or underscore. 3. Identifiers are case sensitive. 4. Identifiers can be of any length. Literals (Constants) It is sequence of characters (includes letters, digits, special characters) that represent constant values to be stored in variable. There are five types of literals, namely Jagadeesh A Y, Dept. of CSE SPCE 10

11 1. Boolean Literals 2. Integer Literals 3. Floating Literals 4. Character Literals 5. String Literals Boolean Literals Boolean literals have values TRUE or FALSE In Java, 1 is not considered as TRUE & 0 is not considered as FALSE as like in C. Example: boolean b = TRUE Boolean values should not be enclosed in double quotes. Integer Literals Integers are whole numbers do not contain fractional part. The sign + & - is used to denote positive and negative numbers respectively. The integer literals are classified into following categories Decimal Literals (Base 10 Numbers) It is a combination of digits between 0 to 9. The sign + and - is used to denote positive and negative numbers respectively. Ex: 10, 200, etc. are valid decimal literals. Octal Literals (Base 8 Numbers) It is a combination of digits between 0 to 7 with a prefix 0 (zero). It is a base 8 number with prefix 0. Ex: 0123, 0345, 067 etc. are valid octal literals. Hexadecimal Constant (Base 16 number) It is a combination of numbers between 0 to 9 along with A to F or a to f. Hexadecimal numbers must precede with 0X or 0x. Ex: 0X12AF, 0x34EA etc are valid hexadecimal number. Jagadeesh A Y, Dept. of CSE SPCE 11

12 Floating Literals Floating point literals are base 10 numbers with fractional part. The sign + and - is used to denote positive and negative floating point numbers respectively. The floating literals are classified into following categories float The suffix F or f is appended to value to designate data type as float. Example: float x = 3.142f double By default floating point literal is double. The suffix D or d is appended to value to designate data type as double. Example: double d = d Character Literals It is a single 16-bit Unicode character. Character literals are enclosed in single quotes. Example: char ch = 'A'; String Literals Character A a Unicode Value '\u0041' It is sequence of characters. '\u0061' 0 '\u0030' String literals should be enclosed in double quotes. Example: String str = Hello ; Jagadeesh A Y, Dept. of CSE SPCE 12

13 Operators Operators are a character which denotes type of operation to be performed on the operands. There three types of operators based on number of operands, namely 1. Unary operators (Uses one operand): ++a, a++, --a, a-- 2. Binary operators (Uses two operands): a+b, a*b 3. Ternary operators (Uses three operands): a?b:c Separators Arrays Separators in Java programming is used to define the structure of the program's. Java supports below separators Separator Name Use. Period Separate packages, access class members, Comma Separate variables, parameters ; Semicolon Terminate statements. ( ) Parenthesis Actual parameters, Formal parameters Braces To define block, function definition, class [ ] Brackets To declare array It is collection of elements of similar data type. There are two types of arrays, namely 1. One Dimensional Arrays 2. Multi-Dimensional Arrays One Dimensional Array It is collection of elements of similar data type, where elements are stored in contiguous memory locations. By default, array locations are initialized to zero. Jagadeesh A Y, Dept. of CSE SPCE 13

14 Syntax to declare one dimensional array data_type[] variable_name; OR data_type []variable_name; OR data_type variable_name[]; Memory allocation to array Where data_type[] variable_name = new data_type[size]; data_type short, int, long, float, double variable_name valid identifier new keyword size size of the array Example: int[] array1 = new int[10]; Array Initialization In the above example, array1 holds 10 values. Initialization is a method in which array is declared and values are assigned to array at the same line. Method 1 Example: int array1[] = 31, 28, 31, 30, 31, 30, 31, 31; Program 1: To initialize one dimensional array & print elements. public class OneDimensionalArray public static void main(string[] args) Output: int array1[] = 5, 6, 7, 8, 9; for(int i=0; i<4; i++) System.out.println(array1[i]); Jagadeesh A Y, Dept. of CSE SPCE 14

15 Method 2 Program 2: To initialize one dimensional array & print elements. public class OneDimensionalArray public static void main(string[] args) Output: int[] array1 = new int[] 5, 6, 7, 8, 9; for(int i=0; i<4; i++) Two Dimensional Arrays System.out.println(array1[i]); It is collection of elements of similar data types, where elements are arranged in rows and columns. By default, array locations are initialized to zero. Syntax to declare one dimensional arrays data_type[][] variable_name; OR data_type [][]variable_name; OR data_type variable_name[][]; Memory allocation to array Where data_type[][] variable_name = new data_type[row_size][column_size]; data_type short, int, long, float, double variable_name valid identifier new keyword row_size number of rows column_size number of columns Example: int[][] array1 = new int[3][3]; In the above example, array1 holds three rows & three columns. Jagadeesh A Y, Dept. of CSE SPCE 15

16 Method 1 Example: int array1[][] = 31, 28, 31, 30, 31, 30,, 31, 31, 30 ; Program 3: To initialize two dimensional array & print elements. public class TwoDimensionalArray public static void main(string[] args) Output: Method 2 int[][] array1 = new int[][] 31, 28, 31, for(int i = 0; i<3; i++) ; for(int j=0; j<3; j++) 30, 31, 30,, 31, 31, 30 System.out.print("\t"+array1[i][j]); System.out.println(); Example: int[][] array1 = new int[][] 31, 28, 31, 30, 31, 30, 31, 31, 30 ; Jagadeesh A Y, Dept. of CSE SPCE 16

17 Program 4: To initialize two dimensional array & print elements. public class TwoDimensionalArray Output: public static void main(string[] args) int array1[][] = 31, 28, 31, 30, 31, 30,, 31, 31, 30 ; for(int i = 0; i<3; i++) for(int j=0; j<3; j++) Assigning Values System.out.print("\t"+array1[i][j]); System.out.println(); The values to variables are assigned using assignment operator (=). Example: int a = 100; In above example, value 100 is stored in the variable a. Access Modifiers Access modifiers are used to specify visibility of class, variables, methods & constructors. There are four access modifiers in java, namely 1. Default 2. Private 3. Public 4. Protected Jagadeesh A Y, Dept. of CSE SPCE 17

18 Access Modifiers Default Private Protected Public Accessible inside the class Yes Yes Yes Yes Accessible within the subclass inside the same package Yes No Yes Yes Accessible outside the package No No No Yes Accessible within the subclass outside the package Operators and Expressions No No No Yes An operator is a symbol that specifies the type of operation to be performed on variables or values. An expression is a sequence of operators & operands, which will be evaluated to single constant value. Precedence or priority: of an operator decides which terms should be evaluated first in any expression. For Example: c=a+b*20; In the above expression b*20 will be evaluated first and then a+(result of b*20). Since * (multiplication) is having higher precedence over + (addition). Associativity: When 2 or more operators have same priority, then associativity of an operator tells the direction to evaluate terms i.e. either from left-to-right or right-to-left. For Example: c=a*b-d/20; Here * and / are equal priority, so operators are evaluated from left-to-right. Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Arithmetic Relational Operators Bitwise Operators Logical Operators Assignment Operators Misc. Operators Jagadeesh A Y, Dept. of CSE SPCE 18

19 Arithmetic Operator These operators are used to perform basic arithmetic operations such as addition, subtraction, multiplication, division and remainder. Arithmetic operators operates on two operands, hence they are called as binary operators. An expression which consists of only arithmetic operators & operands is called as arithmetic expression. Ex: a+b, b-c, c/d, x%y, x*y Operators, Precedence & Associativity Operation Operator Precedence Associativity Multiplication * 1 L R Division / 1 L R Remainder % 1 L R Addition + 2 L R Subtraction - 2 L R Modes of arithmetic expressions The arithmetic operators can acts on integer and real number. So based on the type of the operands arithmetic expressions are divided into 3 modes, namely 1. Integer mode arithmetic expression 2. Floating point arithmetic expression 3. Mixed mode arithmetic expression Integer mode arithmetic expression All the operands in an expression must be of type integer. A result of integer expression must be an integer. Ex: 10/2=5, 9*2=18, 4/3=1 (not 1.33) Floating point arithmetic expression All the operands in an expression must be of type floating point numbers or double values. The result of floating point expression must be floating point number. Jagadeesh A Y, Dept. of CSE SPCE 19

20 Ex: 5.0/2.0=2.0, 4.0/3.0=1.333, 4.0/2.0=2.0 (floating) Mixed mode arithmetic expression An expression that has operands of different data types. Always the data of lower rank will be converted to higher rank.(refer type conversion) Example int a = 4, b = 2; float x = 2, y; y = (a * b) / x; = (4.0 * 2.0) / 2.0; = 4.0; Program 5: To perform arithmetic operations. public class Addition public static void main(string args[]) Output: int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("b-- = " + (a--) ); System.out.println("d++ = " + (d++) ); System.out.println("++d = " + (++d) ); Jagadeesh A Y, Dept. of CSE SPCE 20

21 Relational operators Used to compare the relationship between two operands Result of relational operator is either TRUE or FALSE. Two operands may be constants, variable or an expression. Operators, Precedence & Associativity Operation Operator Precedence Associativity Lesser than < 1 L R Greater than > 1 L R Lesser than or equal to <= 1 L R Greater than or equal to >= 1 L R Equal to == 2 L R Not equal to!= 2 L R Program 6: To perform relational operations. public class RelationalOperators public static void main(string args[]) int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a!= b = " + (a!= b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); Output: Jagadeesh A Y, Dept. of CSE SPCE 21

22 Bitwise Operators The bitwise operator manipulates the data using bits. Manipulation of individual bit is carried out in a machine language. Java provides six bitwise operators. Bitwise operator works on int, short, long, byte, char type data. Operators, Precedence & Associativity Operation Operator Precedence Associativity NOT ~ 1 L R Left Shift << 2 L R Right Shift >> 2 L R Right Shift Zero >>> 2 L R And & 3 L R XOR ^ 4 L R OR 5 L R The result of bitwise AND is 1, when both bits are 1 otherwise result is 0. The result of bitwise OR is 0, when both bits are 0 otherwise result is 1. The result of bitwise exclusive OR is 1, when both bits are different. The result of bitwise exclusive OR is 0, when both bits are different. The bitwise complement operator reverses the state of each bit If bit is 1, then complement operator reverses to 0. If bit is 0, then complement operator reverses to 1. Truth Table A B A&B A B A^B ~A Jagadeesh A Y, Dept. of CSE SPCE 22

23 Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows: a = a = a = b = b = b = b = a&b = a b = a^b = ~b = Left shift operator (<<) Where The left shift operator shifts data by a specified number of bit positions towards left. Syntax: <variable><shift operator><nob>; variable int or char data type variable shift operator << nob number of bits to be shifted Example: int a = 64, b; b = a << 1; Answer? Right shift operator (>>) Where The right shift operator shifts data by a specified number of bit positions towards right. The shifted bits are filled by MSB. Syntax <variable><shift operator><nob>; variable int or char data type variable shift operator >> nob number of bits to be shifted Example: int a = 64, b; b = a >> 1; Answer? Unsigned Right shift operator (>>>) The right shift operator shifts data by a specified number of bit positions towards right. The shifted bits are filled by Zero. Syntax: (variable) >>>(nob); Jagadeesh A Y, Dept. of CSE SPCE 23

24 Where variable int or char data type variable shift operator >> > nob number of bits to be shifted Example: int a = 64, b; b = a << 1; Program 7: To perform bitwise operations. public class Bitwise public static void main(string args[]) int a = 60; /* 60 = */ Output: int b = 13; /* 13 = */ int c = 0; c = a & b; /* 12 = */ System.out.println("a & b = " + c ); c = a b; /* 61 = */ System.out.println("a b = " + c ); c = a ^ b; /* 49 = */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = */ System.out.println("~a = " + c ); c = a << 2; /* 240 = */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = */ System.out.println("a >>> 2 = " + c ); Jagadeesh A Y, Dept. of CSE SPCE 24

25 Logical Operators Logical operators are used to combine two or more relational expressions. The result of logical operator should be either TRUE or FALSE. A B A&&B A B!A FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE Program 8: To perform logical operations. public class Logical_Operators public static void main(string args[]) Output: boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a b = " + (a b) ); System.out.println("!(a && b) = " +!(a && b)); Logical Expressions An expression involving operands which are bind together with suitable logical operators is called as logical operators. Example: (a > b && a > c), (a > b a > c) Assignment Operator The operator which is used to assign the data or result of an expression in to a variable is called assignment operator. Jagadeesh A Y, Dept. of CSE SPCE 25

26 The assignment operator is =. Example 1. x = 10; the value 10 is assigned to the variable x. 2. x = y+z; the value (y+z) is stored in the variable x. Types of assignment expression 1. Simple assignment statement Syntax: variable = expression; where Example: a = 40; expression may be a constant or variable or expression. a = b + c; 2. Shorthand assignment operator = assignment operator. It is a compact way of writing expressions in an assignment statement. Syntax: <variable> <operator>= <expression>; where variable is a valid C identifier. operator arithmetic or bitwise operator. expression constant or variable or expression Example: a += 10; (which is nothing but a = a+10;) x -= 2; (which is nothing but x = x-2;) 3. Multiple assignment statement Assigns same value to two or more variables in one statement. The multiple assignment operators are right associative. Example: i = j = k = 20; The? Operator It is called as conditional or ternary operator. It operates on three operands. The objective of conditional operator is to decide which value should be assigned to variable. Syntax: variable = (expression)? Value if true : Value if false; Jagadeesh A Y, Dept. of CSE SPCE 26

27 Program 9: To evaluate ternary operator. public class Ternary public static void main(string args[]) int a, b; a = 10; b = (a == 1)? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10)? 20: 30; System.out.println( "Value of b is : " + b ); Operators Precedence Operators Operation Priority Associativity [ ] Array Index () Method Call 1 L R. Member Access a++ Postfix Increment a-- Prefix Decrement 2 R L ~ Bitwise Negation ++a --a + Unary Plus - Unary Minus 3 R L! Logical NOT new Jagadeesh A Y, Dept. of CSE SPCE 27

28 () typecasting * Multiplication / Division 4 L R % Remainder + Addition or String Concatenation - Subtraction << Left Shift >> Right Shift >>> Unsigned Right Shift < Lesser than <= Lesser than or equal to > Greater than >= Greater than or equal to instance of Type comparison == Is equal to 5 L R 6 L R 7 L R!= Not equal to 8 L R & Bitwise AND 9 L R ^ Bitwise Exclusive OR 10 L R Bitwise OR 11 L R && Logical AND 12 L R Logical OR 13 L R Jagadeesh A Y, Dept. of CSE SPCE 28

29 ? : Ternary or Conditional 14 R L == Assignment *=, /=, %=, +=, -=, &=, ^=, =, <<=, >>=, >>>= Short Assignment 15 R L Type Casting The process of conversion of data type of an operand is called as type conversion. For example, if operands in an expression are of different data types, then all operands must be converted into single data type before evaluating an expression. There are two types of type conversion, namely 1. Implicit Conversion (Promotion or Widening) 2. Explicit Conversion (Narrowing) Implicit Type Conversion The process of conversion of data from lower rank to higher rank automatically. Here, Automatic Type casting takes place when, the two types are compatible the target type is larger than the source type Program 10: To perform Implicit type conversion. public class Explicit public static void main(string[] args) int i = 100; long l = i; float f = l; //no explicit type casting required //no explicit type casting required System.out.println("Int value "+i); System.out.println("Long value "+l); System.out.println("Float value "+f); Jagadeesh A Y, Dept. of CSE SPCE 29

30 Output: Explicit type conversion The forcible conversion of data from one data type to another data type is known as explicit conversion. Syntax: (data type)expression; where data type required data type expression can be operand or variable or constant Program 11: To perform Explicit type conversion. public class Implicit public static void main(string[] args) double d = ; long l = (long)d; //explicit type casting required int i = (int)l; //explicit type casting required System.out.println("Double value "+d); System.out.println("Long value "+l); System.out.println("Int value "+i); Control Statements These statements control the flow of execution of the program based upon conditions. Java supports two control statements, namely 1. if 2. switch if statement It is a conditional statement, which executes or skips set of instructions based upon result of the evaluated condition. Jagadeesh A Y, Dept. of CSE SPCE 30

31 Variants of if statement 1. Simple-if 2. if-else 3. nested if 4. else-if ladder Simple if If condition evaluates to true, then simple or block statements are executed. Syntax: If condition evaluates to false, then simple or block statements are skipped. It is also called as one way decision or selection statement. if( condition ) statement1; statement2; where if -> is a keyword, must be written in lower case condition -> logical expression results in either TRUE or FALSE statement -> simple or compound statement Program 13: To implement simple if statement. public class SimpleIf public static void main(string[] args) int a = 10, b=20; if( a == 10 ) System.out.println("a equal to 10"); if( b == 20 ) System.out.println("b is equal to 20"); if-else It is called as two way selection. If condition evaluates to true, then statements belongs to if part are executed. Jagadeesh A Y, Dept. of CSE SPCE 31

32 Syntax Where If condition evaluates to false, then statements belongs to else part are executed. if ( condition ) else statement 1; statement 2; statement 3; statement 4; if & else -> are keywords, must be written in lower case condition -> a logical expression evaluates to either TRUE or FALSE statement 1 & 2 -> may be simple or compound statement, executed when condition is TRUE statement 3 & 4 -> may be simple or compound statement, executed when condition is FALSE Program 14: To find smallest of two numbers. public class SimpleIf public static void main(string[] args) int a = 10, b=20; if( a < b ) else System.out.println("a is lesser than b"); System.out.println("b is lesser than a"); Jagadeesh A Y, Dept. of CSE SPCE 32

33 Nested If Syntax Enclosing an if or if-else statement within another if or if-else statement is called nested if statement. It is also called as multi-way decision or selection statement. In this case, nesting will takes place only in if part. if(condition1) else if(condition2) else statement1; statement3; statement2; Program 15: To find largest of three numbers. public class Nested_If C\n"); public static void main(string[] args) int a = 10, b = 20, c = 30; if( a > b ) if( a > c ) System.out.println("A is largest among B & Jagadeesh A Y, Dept. of CSE SPCE 33

34 if( b > a ) if( b > c ) System.out.println("B is largest among A & C\n"); if( c > a ) if( c > b ) System.out.println("C is largest among A & B\n"); Output: else-if ladder Checks condition in else part. Nesting will takes place only in else part. It is also called as multi-way decision or selection statements. Jagadeesh A Y, Dept. of CSE SPCE 34

35 Syntax where if( condition1 ) statement1; else if( condition2 ) staement2; else if( condition3 ) else staement3; statement4; if & else -> are keywords, must be written in lower case else if -> else and if are separate keywords, must provide space between them. statement -> may be a simple or compound statements executed depending on the result of conditions. Working of else-if ladder If the result of logical expression given in if is FALSE, control will enters into else part. Checks one more conditions given in else if, if the result is TRUE then executes immediately following simple or compound statements, then comes out of entire else- if ladder. If none of the condition is TRUE, then statements in else part will be executed. Jagadeesh A Y, Dept. of CSE SPCE 35

36 Program 16: To classify people based on height. import java.util.scanner; public class Else_If public static void main(string[] args) Output: int ht; Scanner in = new Scanner(System.in); System.out.println("Enter height in cms:\n"); ht = in.nextint(); if(ht < 165) System.out.println("Dwarf\n"); else if(ht>165 && ht <175) System.out.println("Average\n"); else if(ht>175 && ht <180) System.out.println("Normal\n"); else if(ht>180 && ht <185) else Switch statement System.out.println("Tall\n"); System.out.println("Abnormal\n"); It is a conditional statement used to select one case depending upon the value of expression. It is also called as multi-way decision or selection. Jagadeesh A Y, Dept. of CSE SPCE 36

37 Syntax switch(choice/expression) where case label1: case label2: break; break; block1; block2; default: default block; break; switch, case, break, default -> are keywords must be written in lower case choice/expression -> must be integer or character, we can also give expression and it should produce valid integer result. block-> may be simple or compound statements belongs to particular case. In the above syntax, Give integer or character or valid expression which yields integer result within the parenthesis. Depending on the value of choice or expression a particular case label among many alternatives will be selected. A simple or compound statement of selected case label will be executed. Each case must end with break statement. If break statement is not used, then subsequent cases will be executed till it encounters break statement. After executing the break statement of particular case label, then control comes out of the switch body. If the value of the choice or expression do not matches to any of the case label, then the statements given in default will be executed. Program 17: To simulate simple calculator using switch. import java.util.scanner; public class Swithc_Calulator Jagadeesh A Y, Dept. of CSE SPCE 37

38 public static void main(string[] args) int choice = 0, a, b, res = 0; Scanner in = new Scanner(System.in); System.out.println("Enter Two Numbers\n"); a = in.nextint(); b = in.nextint(); System.out.println("1. Addition\n"); System.out.println("2. Subtraction\n"); System.out.println("3. Mulitplication\n"); System.out.println("4. Division\n"); System.out.println("5. Quit\n"); System.out.println("Enter your choice\n"); choice = in.nextint(); switch(choice) is" +res +"\n"); case 1: res = a + b; System.out.println("Addition of two numbers break; case 2: res = a - b; System.out.println("Subtraction of two numbers is" +res +"\n"); break; case 3: res = a * b; System.out.println("Subtraction of two numbers is" +res +"\n"); break; case 4: if ( b == 0) System.out.println("Run Error:\nDivision Not Possible\n"); else Time Jagadeesh A Y, Dept. of CSE SPCE 38

39 numbers is" +res +"\n"); Iteration statements res = a * b; System.out.println("Subtraction of two break; default: System.out.println("Invalid Choice\n"); These statements execute set of statements repeatedly until condition is met. Java's iterative statements are 1. for 2. while 3. do-while while Syntax It repeatedly executes simple or block statement until condition is true. while( expression ) where statements; while -> is a keyword must be written in lower case statements -> may be simple or compound statement expression -> is a logical expression that results in either TRUE or FALSE Program 18: To print numbers from 1 to 10. public class While_Print_Numbers public static void main(string[] args) int n = 1; while( n <= 10) Jagadeesh A Y, Dept. of CSE SPCE 39

40 do-while System.out.println(n); n++; A do-while loop is similar to a while loop, but do-while loop is guaranteed to execute at least one time. Syntax do statements; while( expression ); where do & while -> is a keyword must be written in lower case statements -> may be simple or compound statement expression -> is a logical expression that results in either TRUE or FALSE Program 19: To print numbers from 10 to 20 using do-while public class DoWhile for public static void main(string args[]) int x = 10; do System.out.print("value of x : " + x ); x++; System.out.print("\n"); while( x < 20 ); it is also called as counter-controlled loop / pre-test loop. It is a conditional control statement, executes a set of statement repeatedly for known number of times. Jagadeesh A Y, Dept. of CSE SPCE 40

41 Syntax for( exp1(initialization); exp2(boolean_expression); exp3(update) ) where statements; for -> is a keyword must be written in lower case exp1 -> initializes the loop index before the loop begins exp2 -> is a conditional expression that tests whether the loop index reached the fixed value exp3 -> modifies the loop index after every iteration or post condition statements -> may be a simple or a compound statement Working of for loop First expression exp1 is evaluated. It is executed at the beginning of the for loop, exp1 is evaluated only once in the for loop. Then, exp2 is evaluated to TRUE or FALSE. If the result of exp2 is FALSE, then control comes out of for loop. If the result of exp2 is TRUE, then the body of the loop will be executed. After executing the body of the loop control goes back to the exp3 is evaluated followed by exp2. Again exp2 is evaluated to TRUE or FALSE, and loop repeats till result of exp2 becomes FALSE. Program 20: To print numbers from 1 to 20 using for loop. public class ForLoop public static void main(string args[]) for(int x = 1; x <= 20; x = x+1) Output: System.out.print("value of x : " + x ); System.out.print("\n"); Jagadeesh A Y, Dept. of CSE SPCE 41

42 Enhanced for loop It is an enhanced for loop introduced in JAVA 5.5 version. In each iteration the element in the collection is retrieved & stored in the iteration variable. Then loop is executed with that value. The loop repeats for all elements in the collection. Data type of loop variable must be same as data type of elements in collection. Elements in an array are read in sequential order from beginning to ending. Program 21: To print elements of array using enhanced for loop. public class ForEach public static void main(string[] args) int[] array1 = new int[] 5, 6, 7, 8, 9; for(int x: array1) System.out.println(x); Program 22: To print elements of two dimensional array using enhanced for loop. public class ForEachTwoDimensional public static void main(string[] args) int a[][] = new int[][] 1,2,3, 4,5,6 ; /* Printing Array Elements using nested for loop for(int i = 0; i<2; i++) for(int j = 0; j<3; j++) System.out.println(a[i][j]);*/ /* Printing Array Element Using For Each*/ Jagadeesh A Y, Dept. of CSE SPCE 42

43 for(int x[]: a) for(int y: x) System.out.println(y); Jump Statements Jump statements are used to transfer control to other parts of the program. Java supports three jump statements, namely 1. break 2. continue 3. return Break It can be used to terminate statements in switch. It can be used to exit from loops like for, while, do-while. It can be used as a form of goto. Program 23: To use break in for loop. public class Break public static void main(string[] args) for(int i = 1; ; i++) if(i == 11) break; Output: System.out.println(i); Jagadeesh A Y, Dept. of CSE SPCE 43

44 Program 24: To use break in while loop. public class BreakWhile public static void main(string[] args) Output: int i = 1; while(true) if (i == 11) break; System.out.println(i); i++; Program 25: To use break as goto. class BreakGoto public static void main(string args[]) break"); int i=10; one: two: System.out.println("Statement if(i==10) break two; before Jagadeesh A Y, Dept. of CSE SPCE 44

45 System.out.println("This will not get executed"); System.out.println("This is statement block of label one"); Output: Continue It skips statements in an iteration and continues with the next iteration. It transfers control to the conditional statement. Program 26: To print odd numbers from 1 to 10 using continue statement. public class Continue public static void main(string[] args) for(int i=1; i<=10; i++) if(i%2 == 0) continue; System.out.println(i); Return statement It is used to transfer control from called function to calling function. It also terminates execution of program. Program 27: To print numbers from 1 to 10 using return statement. public class Return public static void main(string[] args) Jagadeesh A Y, Dept. of CSE SPCE 45

46 Output: for(int i = 1; ; i++ ) if(i == 10) return; System.out.println(i); Jagadeesh A Y, Dept. of CSE SPCE 46

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Language Fundamentals Summary

Language Fundamentals Summary Language Fundamentals Summary Claudia Niederée, Joachim W. Schmidt, Michael Skusa Software Systems Institute Object-oriented Analysis and Design 1999/2000 c.niederee@tu-harburg.de http://www.sts.tu-harburg.de

More information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Programming. Syntax and Semantics

Programming. Syntax and Semantics Programming For the next ten weeks you will learn basic programming principles There is much more to programming than knowing a programming language When programming you need to use a tool, in this case

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

Introduction to Programming (Java) 2/12

Introduction to Programming (Java) 2/12 Introduction to Programming (Java) 2/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

More information

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS Java language Part 1. Java fundamentals Yevhen Berkunskyi, NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua What Java is? Programming language Platform: Hardware Software OS: Windows, Linux, Solaris,

More information

Java Notes. 10th ICSE. Saravanan Ganesh

Java Notes. 10th ICSE. Saravanan Ganesh Java Notes 10th ICSE Saravanan Ganesh 13 Java Character Set Character set is a set of valid characters that a language can recognise A character represents any letter, digit or any other sign Java uses

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

The Java language has a wide variety of modifiers, including the following:

The Java language has a wide variety of modifiers, including the following: PART 5 5. Modifier Types The Java language has a wide variety of modifiers, including the following: Java Access Modifiers Non Access Modifiers 5.1 Access Control Modifiers Java provides a number of access

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

More information

Java Overview An introduction to the Java Programming Language

Java Overview An introduction to the Java Programming Language Java Overview An introduction to the Java Programming Language Produced by: Eamonn de Leastar (edeleastar@wit.ie) Dr. Siobhan Drohan (sdrohan@wit.ie) Department of Computing and Mathematics http://www.wit.ie/

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Java Programming. Atul Prakash

Java Programming. Atul Prakash Java Programming Atul Prakash Java Language Fundamentals The language syntax is similar to C/ C++ If you know C/C++, you will have no trouble understanding Java s syntax If you don't, it will be easier

More information

.Net Technologies. Components of.net Framework

.Net Technologies. Components of.net Framework .Net Technologies Components of.net Framework There are many articles are available in the web on this topic; I just want to add one more article over the web by explaining Components of.net Framework.

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995 Java Introduc)on History of Java Java was originally developed by Sun Microsystems star:ng in 1991 James Gosling Patrick Naughton Chris Warth Ed Frank Mike Sheridan This language was ini:ally called Oak

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>= Operators in java Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc. There are many types of operators in java which are given below: Unary Operator, Arithmetic

More information

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

More information

CS11 Java. Fall Lecture 1

CS11 Java. Fall Lecture 1 CS11 Java Fall 2006-2007 Lecture 1 Welcome! 8 Lectures Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7-8 Lab Assignments Made available on Mondays Due one week later Monday, 12 noon

More information

Introduction To Java Programming

Introduction To Java Programming Introduction To Java Programming You will learn about the process of creating Java programs and constructs for input, output, branching, looping, as well some of the history behind Java s development.

More information

13 th Windsor Regional Secondary School Computer Programming Competition

13 th Windsor Regional Secondary School Computer Programming Competition SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse

More information

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators, Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators, Expressions, Statements and Arrays. Java technology is: A programming

More information

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail. OOP in Java 1 Outline 1. Getting started, primitive data types and control structures 2. Classes and objects 3. Extending classes 4. Using some standard packages 5. OOP revisited Parts 1 to 3 introduce

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Java: framework overview and in-the-small features

Java: framework overview and in-the-small features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: framework overview and in-the-small features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

Java Programming Fundamentals. Visit for more.

Java Programming Fundamentals. Visit  for more. Chapter 4: Java Programming Fundamentals Informatics Practices Class XI (CBSE Board) Revised as per CBSE Curriculum 2015 Visit www.ip4you.blogspot.com for more. Authored By:- Rajesh Kumar Mishra, PGT (Comp.Sc.)

More information

Core JAVA Training Syllabus FEE: RS. 8000/-

Core JAVA Training Syllabus FEE: RS. 8000/- About JAVA Java is a high-level programming language, developed by James Gosling at Sun Microsystems as a core component of the Java platform. Java follows the "write once, run anywhere" concept, as it

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi... Contents 1 Introduction 3 1.1 Java, the beginning.......................... 3 1.2 Java Virtual Machine........................ 4 1.3 A First Program........................... 4 1.4 BlueJ.................................

More information

Operators in java Operator operands.

Operators in java Operator operands. Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

SECTION II: LANGUAGE BASICS

SECTION II: LANGUAGE BASICS Chapter 5 SECTION II: LANGUAGE BASICS Operators Chapter 04: Basic Fundamentals demonstrated declaring and initializing variables. This chapter depicts how to do something with them, using operators. Operators

More information

4 Programming Fundamentals. Introduction to Programming 1 1

4 Programming Fundamentals. Introduction to Programming 1 1 4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. Today! Build HelloWorld yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. HelloWorld in BlueJ 1. Find BlueJ in the start menu, but start the Select VM program instead (you

More information

An overview of Java, Data types and variables

An overview of Java, Data types and variables An overview of Java, Data types and variables Lecture 2 from (UNIT IV) Prepared by Mrs. K.M. Sanghavi 1 2 Hello World // HelloWorld.java: Hello World program import java.lang.*; class HelloWorld { public

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Output :: /* display results */ System.out.print( Call Duration: );

Output :: /* display results */ System.out.print( Call Duration: ); Introduction 7 : JAVA BASICS Java is object oriented programming language developed by Sun Microsystems in 1991. A company best known for its high-end UNIX workstations. Modelled after C++ Java language

More information

STRUCTURING OF PROGRAM

STRUCTURING OF PROGRAM Unit III MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of Data Abstraction? (a) Reduce Complexity (c) Parallelism Unit III 3.1 (b) Binds together code and data (d) None of the

More information

CompSci 125 Lecture 02

CompSci 125 Lecture 02 Assignments CompSci 125 Lecture 02 Java and Java Programming with Eclipse! Homework:! http://coen.boisestate.edu/jconrad/compsci-125-homework! hw1 due Jan 28 (MW), 29 (TuTh)! Programming:! http://coen.boisestate.edu/jconrad/cs125-programming-assignments!

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Java enum, casts, and others (Select portions of Chapters 4 & 5) Enum or enumerates types Java enum, casts, and others (Select portions of Chapters 4 & 5) Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT.

NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT. NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT. Object and Classes Data Abstraction and Encapsulation Inheritance Polymorphism Dynamic Binding Message Communication Objects are the basic runtime entities in

More information

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course JAC444 - Lecture 1 Introduction to Java Programming Language Segment 4 1 Overview of the Java Language In this segment you will be learning about: Numeric Operators in Java Type Conversion If, For, While,

More information

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23. Subject code - CCP01 Chapt Chapter 1 INTRODUCTION TO C 1. A group of software developed for certain purpose are referred as ---- a. Program b. Variable c. Software d. Data 2. Software is classified into

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

Java Programming Language. 0 A history

Java Programming Language. 0 A history Java Programming Language 0 A history How java works What you ll do in Java JVM API Java Features 0Platform Independence. 0Object Oriented. 0Compiler/Interpreter 0 Good Performance 0Robust. 0Security 0

More information

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, java provides control structures

More information

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java Goals Understand the basics of Java. Introduction to Java Write simple Java Programs. 1 2 Java - An Introduction Java is Compiled and Interpreted Java - The programming language from Sun Microsystems Programmer

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

C Programming Class I

C Programming Class I C Programming Class I Generation of C Language Introduction to C 1. In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) 2. In 1970, Ken Thompson created a language

More information

Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena TA: Rochak Sachan JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz) SOFTWARE DEVELOPMENT 1 Operators 2018W (Institute of Pervasive Computing, JKU Linz) OPERATORS Operators are required to form expressions. Depending on the number of operands they take, they are called:

More information

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language Chapter 1 Getting Started Introduction To Java Most people are familiar with Java as a language for Internet applications We will study Java as a general purpose programming language The syntax of expressions

More information

Character Stream : It provides a convenient means for handling input and output of characters.

Character Stream : It provides a convenient means for handling input and output of characters. Be Perfect, Do Perfect, Live Perfect 1 1. What is the meaning of public static void main(string args[])? public keyword is an access modifier which represents visibility, it means it is visible to all.

More information

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP Primitive and Class Types. Yi Hong May 14, 2015 COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

More information

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments Basics Objectives Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments 2 Class Keyword class used to define new type specify

More information

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017

Introduction to Java. Lecture 1 COP 3252 Summer May 16, 2017 Introduction to Java Lecture 1 COP 3252 Summer 2017 May 16, 2017 The Java Language Java is a programming language that evolved from C++ Both are object-oriented They both have much of the same syntax Began

More information

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines

More information