Chapter 7 Java Basics (Part 1) Presented by nuzhat memon
Presented by Nuzhat Memon Introduction to Java  Java is an object-oriented programming language developed at Sun Microsystems, a company a best known for its high-end Unix workstations, in 1991.  Modeled after c++, the java language was designed to be small, simple, fast, efficient and portable across platform and operating systems.  Java is considered as one of the ideal language for distributing executable programs via the WWW and also general purpose programming language for developing programs that are easily usable and portable across different platforms. 2 1991 Small Efficient fast Portable Simple Bytecode (Class file) Windows JVM Linux JVM MAc JVM
Presented by Nuzhat Memon Introduction to Java (continue) | JDK  JDK is used to develop and run Java program  Java includes a set of class libraries that provide basic data types, system input and output capabilities, and other utility functions.  These Basic classes are part of the Java Development Kit (JDK).  JDK has classes to support networking, common internet protocols and user interface toolkit functions. 3 Class libraries Basic data types, System input & output capabilities and other utility functions Development tools JDK (Java Development Kit) Compiler
Presented by Nuzhat Memon Java is Platform Indedpendent  Platform independence is a program’s capability of being moved easily from one computer system to another.  Java is platform independent at both the source and the binary level..  At the source level, java’s primitive data types have consistent sizes across all development platforms.  At binary level, platform independence is possible due to bytecode interpreter. 4 Bytecode (Class file) Windows JVM Linux JVM MAc JVM
Presented by Nuzhat Memon 5 LINUX JVM Windows JVM Java Platform Independent Java source code (*.java) Java compiler (javac) Java bytecode (*.class) JVM - Java Interpreter (java) HelloWorld.java After compilation HelloWorld.class (class file is our bytecode) JVM understand only bytecode MAC JVM
Presented by Nuzhat Memon JVM (Java Virtual Machine) | Bytecode  Program written in java are compiled into machine language for a computer that doesn’t really exist. This is so-called “virtual” computer is known as the Java Virtual Machine (JVM).  The machine language for the java virtual machine is called java bytecode.  Different java bytecode interpreter is needed for each type of computer.  Advantage - Platform independent - Java binary files are actually in a form called bytecodes that is not specific to any one processor or any operating system.  The only disadvantage of using bytecodes is its slow execution speed.  There are tools available to convert java bytecodes into native code. Native code is faster to execute, but then it does not remain machine independent. 6 Compiler Faster Execution Speed Platform Dependent Interpreter Slow Execution Speed Platform Independent JVM Java Bytecode
Presented by Nuzhat Memon Which of the following is missing from compilation process to execute java application ? 7 JVM - Java Interpreter(Java) Java Compiler (Javac) Java Source File (usually have extension.java) When we create a Java Program File, we usually save it as filename.java which is called source code And then we will Compile our source code using the java compiler javac Compiler will translate the whole program into machine language known as bytecode in java and creates a file with extension .class Java Interpreter or JVM will read the bytecode line by line and Run the class file on different OS or hardware Java Bytecode file (*.class) When java program created java source file using text editor, which file extension should be given? .java When the program gets compiled without error, compiler creates a file with which extension ?.class By using which extension file java interpreter bytecode executes? java Which of the following way user can run “abc.java” source program through the application using java interpreter? Java abc To compile the java program we have to type what in front of source filename? javac filename.java
Presented by Nuzhat Memon Create Java application using SciTE editor  Start SciTE application.  Select File  New and type the java program.  To save the file, select File Save with filename.java  Compile source program using Tools Compile (Ctrl+F7)  If the program is compiled without any error, execute it using Tools  Go (F5) 8
Presented by Nuzhat Memon Structure Of A Java Program + In Java program, text in angle bracket < and > is used as a place holder. + Java consists of function header and the sequence of statements enclosed between braces { and } public class <class-name> { <optional – variable – declarations – and –methods> public static void main(String [ ] args) { <statements> } <optional – variable – declarations – and – methods> } Java source file & class name must be same System.out.print() and System.out.println() both methods are used to display results 9
Presented by Nuzhat Memon Overview 10 byte age = 10 short number = 20 int count = 30 long area = 1234567 float pi = 3.14 double rate = 100.75 char ch = ‘n’ boolean Check = true Data Type Variable Literal Expression A + B * C //Comments
Presented by Nuzhat Memon Data Types + Java supports 8 primitive data types. + Data Types – byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes), float (4 bytes), double (8 bytes), char (2 bytes), Boolean (1 byte) 11 byte, short, int, long Integer Store integer value float, double Float Store floating point value Char Character Single Unicode character boolean Boolean Store true/false logical value
Presented by Nuzhat Memon 12 Data type numbers character 2 bytes char c=‘A’ integers Eg 17,-38488,0 byte 1 byte short 2 bytes int 4 bytes long 8 bytes long num=4l real numbers Eg 5.8 ,-129.35, 3.14 Real numbers in Java are compliant with IEEE 754 float 4 bytes float Pi=3.14f double 8 bytes double var=0.1234567 boolean 1 byte boolean b=true I n t e g e r byte 1 -128 to 127 short 2 -32768 to 32767 int 4 -2147483648 to 2147483647 long 8 -263 to 263-1 R e a l float 4 Upto 7 significant digits double 8 Upto 15 significant digits char 2 16 bit unicode character boolean 1 True , false Default value is false (default data type of floating point literal) Signed values : -2 b-1-1 to 2 b-1 -2 b-1 to 2 b-1-1 Unsigned values: 0 to 2 b-1 0 to 2 b-1 -1 byte : -2 8-1 to 2 8-1-1 -2 7 to 2 7-1 -128 to 127 short : -2 16-1 to 2 16-1-1 -2 15 to 2 15-1 -32768 to 32767 int : -2 32-1 to 2 32-1-1 -2 31to 2 31-1 -2147483648 to 2147483647 long: -2 64-1 to 2 64-1-1 -2 63 to 2 63-1 -9223372036854775808 to 9223372036854775807 char : 0 to 2 16-1-1 0 to 215-1 0 to 65535 boolean: True, false
Presented by Nuzhat Memon Variable + A name used to refer to the data stored in memory is called a variable + Syntax <datatype-name> {variable-names}; + Example int marks; | float rate; | char grade; RULES TO DEFINE VARIABLE NAME • It must begin with an alphabet, underscore (_) or dollar sign ($). • No spaces are allowed in variables : birth date, my school project • It cannot be a reserved word : class, public, static,void if etc • Legal variable names : birth_date, result, CallCost, top5students, date, $price • Illegal variable names : 4me, %discount, birth date 13
Presented by Nuzhat Memon Guidelines For Naming Variables + Choose meaningful variable names. (calculateArea, calculateCallCost) + Java allows variable name of any length. + Separate words with underscore. Ex : birth_date + Java is case-sensitive. So upper case and lower case are considered to be different. Example variable X is different from the variable x and a nuzhat is not a Nuzhat or NUZHAT. + Names of classes to begin with upper case. + Names of variables and of methods begin with lower case. Referred to as camel case. Ex : birthDate, callCost 14
Presented by Nuzhat Memon Variable Types There are 3 kinds of variables : 1) Instance variables 2) Class/Static variables 3) Local variables 15 class Demo{ public static void main(String args[]){ int age=15; float pi=3.14; static int count=0; void display(){ int total=50; System.out.println(“Hello”); } } } Instance variables local variable Class variable
Presented by Nuzhat Memon Literals- A name used for a constant value is known as literal. Different kinds of literals in java for number, character, string and boolean values. 16 Numeric Literals Boolean Literals True and False default value is False Character Literals 16-bit Unicode characters enclosed in ‘ ‘ Example ‘a’, ‘#’, ‘3’ String Literals sequence of characters enclosed in “” Example “hello”, Literals Integer literals Whole numbers Real number literal Floating point literals standard 12.37 scientific 0.1237 x 102 0.1237e2 Escape character : n : New Line t : Tab f : Form Feed b : Backspace r : Carriage return decimal Base 10 (0 to 9) 75,75L,-75 octal Base 8 (0 to 7) 075 hexadecimal Base 16 (0 to 9, A to F) 0x45 or 0X45 Unicode integer literals u000 ufff Binary number Digits 0 or 1 0b or 0B
Presented by Nuzhat Memon Comments Comments in a program are for human readers only Comments are entirely ignored by the computer. They are not compiled or interpreted in java Java supports 3 types of comments : 1) Single line comment // All the text up to the end of the line is ignored. 2) Multi line comment /* ..... */ Comments cannot be nested (comment inside a comment) 3) Documentation comment /** ..... */ (creating API documentation from the code, javadoc system) 17
Presented by Nuzhat Memon Expression + The basic building blocks of expressions are literals, variables and function calls. + More complex expressions can be built up by using operators to combine simpler expression. + Operators include arithmetic operators (+, -, *, /, %), comparison operators (>, <, =, …), logical operators (and, or, not…) A + B * C (A + B) * C 18
Presented by Nuzhat Memon calculate simple and compound interest class CalculateInterest { public static void main(String args[]) { double p=10000; double r=4.5; double n=10; double si,ci; si = (p*r*n)/100; ci= si + p; System.out.println(“Simple interest is “ + si); System.out.println(“Compound interest is “ + ci); } } 19 1)Save the file as CalculateInterest.java on desktop 2) Open command prompt and type cd desktop javac CalculateInterest.java java CalculateInterest
Presented by Nuzhat Memon Operators + Operators are special symbols used to build an expression. 20 Operators 1. Arithmetic Operators 2. Comparison operators 3. Logical Operators 4, Conditional operators 5, Assignment operator
Presented by Nuzhat Memon 21 Basic arithmetic operators are +, – , * , / , % (Modulus) All these operators are binary, they take 2 operands. Used for byte, short, int, long, float, double Arithmetic Operators Operator Meaning Eg + Addition 2+8 – Subtraction 2-8 * Multiplication 2*8 / Division 8/2 % Reminder 8%3 ++ is increment operator, which adds 1. -- is decrement operator, which subtracts 1. Increment/Decrement Operators ++x pre-increment x++ post-increment --x pre-decrement x-- post-decrement Comparison operators are known as relational operators. Comparison Operators Operator Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to Logical operators known as boolean operators. Logical operators : AND, OR, XOR (Exclusive OR), NOT Operator Sign Use AND && Used to combine two values – AND OR || Used to combine two values – OR XOR ^ It returns true only if operands are different NOT ! Operant is true, result is false & vice versa Also known as ternary operator. Conditional operator uses 3 operands. It uses two symbols ? and : in the expression. Syntax : (boolean-expr) ? (expr1) : (expr2) To assign a value to variable by using the assignment operator ‘=’ Syntax: <variable> = <expression>; Conditional Operator Operators Arithmetic Operator Increment and Decrement Operator Comparison Operator Logical Operator Assignment Operator
Presented by Nuzhat Memon Looping Java supports 3 types of looping constructs: (1) for loop + Entry controlled or pre-test loop constructs. + for loop are executed if condition is true. + It is used when numbers of iterations are pre-defined. (2) while loop + Entry controlled or pre-test loop constructs. + Loop are executed if condition is true. + It is used when number of iterations are not pre-determined. (3) do...while loop + Exit controlled or post-test loop constructs. + It repeats the loop if the test condition is true. + Here, statements of loop are executed at least once. 22
Presented by Nuzhat Memon Statement (1) IF STATEMENT + Used in a program to take one of two alternative coursed of action, depending upon Boolean valued expression. (2) SWITCH STATEMENT + Used when there are many alternative actions to be taken depending upon the value of a variable or expression. + Test expression should be of the type byte, char, short or int or enum 23
Presented by Nuzhat Memon Statement BREAK STATEMENT + Used to transfer the control outside switch/loop structure. + It is used to exit the loop. + It is jumps outside the nearest loop containing this statement. CONTINUE STATEMENT + It is used to skip the following statement in a loop and continue with the next iteration. 24
Presented by Nuzhat Memon Thanks! Any questions? You can find me at: + nuzhatmemon.com + Nuzhat.memon@gmail.com 25

Std 12 Computer Chapter 7 Java Basics (Part 1)

  • 1.
    Chapter 7 Java Basics (Part1) Presented by nuzhat memon
  • 2.
    Presented by NuzhatMemon Introduction to Java  Java is an object-oriented programming language developed at Sun Microsystems, a company a best known for its high-end Unix workstations, in 1991.  Modeled after c++, the java language was designed to be small, simple, fast, efficient and portable across platform and operating systems.  Java is considered as one of the ideal language for distributing executable programs via the WWW and also general purpose programming language for developing programs that are easily usable and portable across different platforms. 2 1991 Small Efficient fast Portable Simple Bytecode (Class file) Windows JVM Linux JVM MAc JVM
  • 3.
    Presented by NuzhatMemon Introduction to Java (continue) | JDK  JDK is used to develop and run Java program  Java includes a set of class libraries that provide basic data types, system input and output capabilities, and other utility functions.  These Basic classes are part of the Java Development Kit (JDK).  JDK has classes to support networking, common internet protocols and user interface toolkit functions. 3 Class libraries Basic data types, System input & output capabilities and other utility functions Development tools JDK (Java Development Kit) Compiler
  • 4.
    Presented by NuzhatMemon Java is Platform Indedpendent  Platform independence is a program’s capability of being moved easily from one computer system to another.  Java is platform independent at both the source and the binary level..  At the source level, java’s primitive data types have consistent sizes across all development platforms.  At binary level, platform independence is possible due to bytecode interpreter. 4 Bytecode (Class file) Windows JVM Linux JVM MAc JVM
  • 5.
    Presented by NuzhatMemon 5 LINUX JVM Windows JVM Java Platform Independent Java source code (*.java) Java compiler (javac) Java bytecode (*.class) JVM - Java Interpreter (java) HelloWorld.java After compilation HelloWorld.class (class file is our bytecode) JVM understand only bytecode MAC JVM
  • 6.
    Presented by NuzhatMemon JVM (Java Virtual Machine) | Bytecode  Program written in java are compiled into machine language for a computer that doesn’t really exist. This is so-called “virtual” computer is known as the Java Virtual Machine (JVM).  The machine language for the java virtual machine is called java bytecode.  Different java bytecode interpreter is needed for each type of computer.  Advantage - Platform independent - Java binary files are actually in a form called bytecodes that is not specific to any one processor or any operating system.  The only disadvantage of using bytecodes is its slow execution speed.  There are tools available to convert java bytecodes into native code. Native code is faster to execute, but then it does not remain machine independent. 6 Compiler Faster Execution Speed Platform Dependent Interpreter Slow Execution Speed Platform Independent JVM Java Bytecode
  • 7.
    Presented by NuzhatMemon Which of the following is missing from compilation process to execute java application ? 7 JVM - Java Interpreter(Java) Java Compiler (Javac) Java Source File (usually have extension.java) When we create a Java Program File, we usually save it as filename.java which is called source code And then we will Compile our source code using the java compiler javac Compiler will translate the whole program into machine language known as bytecode in java and creates a file with extension .class Java Interpreter or JVM will read the bytecode line by line and Run the class file on different OS or hardware Java Bytecode file (*.class) When java program created java source file using text editor, which file extension should be given? .java When the program gets compiled without error, compiler creates a file with which extension ?.class By using which extension file java interpreter bytecode executes? java Which of the following way user can run “abc.java” source program through the application using java interpreter? Java abc To compile the java program we have to type what in front of source filename? javac filename.java
  • 8.
    Presented by NuzhatMemon Create Java application using SciTE editor  Start SciTE application.  Select File  New and type the java program.  To save the file, select File Save with filename.java  Compile source program using Tools Compile (Ctrl+F7)  If the program is compiled without any error, execute it using Tools  Go (F5) 8
  • 9.
    Presented by NuzhatMemon Structure Of A Java Program + In Java program, text in angle bracket < and > is used as a place holder. + Java consists of function header and the sequence of statements enclosed between braces { and } public class <class-name> { <optional – variable – declarations – and –methods> public static void main(String [ ] args) { <statements> } <optional – variable – declarations – and – methods> } Java source file & class name must be same System.out.print() and System.out.println() both methods are used to display results 9
  • 10.
    Presented by NuzhatMemon Overview 10 byte age = 10 short number = 20 int count = 30 long area = 1234567 float pi = 3.14 double rate = 100.75 char ch = ‘n’ boolean Check = true Data Type Variable Literal Expression A + B * C //Comments
  • 11.
    Presented by NuzhatMemon Data Types + Java supports 8 primitive data types. + Data Types – byte (1 byte), short (2 bytes), int (4 bytes), long (8 bytes), float (4 bytes), double (8 bytes), char (2 bytes), Boolean (1 byte) 11 byte, short, int, long Integer Store integer value float, double Float Store floating point value Char Character Single Unicode character boolean Boolean Store true/false logical value
  • 12.
    Presented by NuzhatMemon 12 Data type numbers character 2 bytes char c=‘A’ integers Eg 17,-38488,0 byte 1 byte short 2 bytes int 4 bytes long 8 bytes long num=4l real numbers Eg 5.8 ,-129.35, 3.14 Real numbers in Java are compliant with IEEE 754 float 4 bytes float Pi=3.14f double 8 bytes double var=0.1234567 boolean 1 byte boolean b=true I n t e g e r byte 1 -128 to 127 short 2 -32768 to 32767 int 4 -2147483648 to 2147483647 long 8 -263 to 263-1 R e a l float 4 Upto 7 significant digits double 8 Upto 15 significant digits char 2 16 bit unicode character boolean 1 True , false Default value is false (default data type of floating point literal) Signed values : -2 b-1-1 to 2 b-1 -2 b-1 to 2 b-1-1 Unsigned values: 0 to 2 b-1 0 to 2 b-1 -1 byte : -2 8-1 to 2 8-1-1 -2 7 to 2 7-1 -128 to 127 short : -2 16-1 to 2 16-1-1 -2 15 to 2 15-1 -32768 to 32767 int : -2 32-1 to 2 32-1-1 -2 31to 2 31-1 -2147483648 to 2147483647 long: -2 64-1 to 2 64-1-1 -2 63 to 2 63-1 -9223372036854775808 to 9223372036854775807 char : 0 to 2 16-1-1 0 to 215-1 0 to 65535 boolean: True, false
  • 13.
    Presented by NuzhatMemon Variable + A name used to refer to the data stored in memory is called a variable + Syntax <datatype-name> {variable-names}; + Example int marks; | float rate; | char grade; RULES TO DEFINE VARIABLE NAME • It must begin with an alphabet, underscore (_) or dollar sign ($). • No spaces are allowed in variables : birth date, my school project • It cannot be a reserved word : class, public, static,void if etc • Legal variable names : birth_date, result, CallCost, top5students, date, $price • Illegal variable names : 4me, %discount, birth date 13
  • 14.
    Presented by NuzhatMemon Guidelines For Naming Variables + Choose meaningful variable names. (calculateArea, calculateCallCost) + Java allows variable name of any length. + Separate words with underscore. Ex : birth_date + Java is case-sensitive. So upper case and lower case are considered to be different. Example variable X is different from the variable x and a nuzhat is not a Nuzhat or NUZHAT. + Names of classes to begin with upper case. + Names of variables and of methods begin with lower case. Referred to as camel case. Ex : birthDate, callCost 14
  • 15.
    Presented by NuzhatMemon Variable Types There are 3 kinds of variables : 1) Instance variables 2) Class/Static variables 3) Local variables 15 class Demo{ public static void main(String args[]){ int age=15; float pi=3.14; static int count=0; void display(){ int total=50; System.out.println(“Hello”); } } } Instance variables local variable Class variable
  • 16.
    Presented by NuzhatMemon Literals- A name used for a constant value is known as literal. Different kinds of literals in java for number, character, string and boolean values. 16 Numeric Literals Boolean Literals True and False default value is False Character Literals 16-bit Unicode characters enclosed in ‘ ‘ Example ‘a’, ‘#’, ‘3’ String Literals sequence of characters enclosed in “” Example “hello”, Literals Integer literals Whole numbers Real number literal Floating point literals standard 12.37 scientific 0.1237 x 102 0.1237e2 Escape character : n : New Line t : Tab f : Form Feed b : Backspace r : Carriage return decimal Base 10 (0 to 9) 75,75L,-75 octal Base 8 (0 to 7) 075 hexadecimal Base 16 (0 to 9, A to F) 0x45 or 0X45 Unicode integer literals u000 ufff Binary number Digits 0 or 1 0b or 0B
  • 17.
    Presented by NuzhatMemon Comments Comments in a program are for human readers only Comments are entirely ignored by the computer. They are not compiled or interpreted in java Java supports 3 types of comments : 1) Single line comment // All the text up to the end of the line is ignored. 2) Multi line comment /* ..... */ Comments cannot be nested (comment inside a comment) 3) Documentation comment /** ..... */ (creating API documentation from the code, javadoc system) 17
  • 18.
    Presented by NuzhatMemon Expression + The basic building blocks of expressions are literals, variables and function calls. + More complex expressions can be built up by using operators to combine simpler expression. + Operators include arithmetic operators (+, -, *, /, %), comparison operators (>, <, =, …), logical operators (and, or, not…) A + B * C (A + B) * C 18
  • 19.
    Presented by NuzhatMemon calculate simple and compound interest class CalculateInterest { public static void main(String args[]) { double p=10000; double r=4.5; double n=10; double si,ci; si = (p*r*n)/100; ci= si + p; System.out.println(“Simple interest is “ + si); System.out.println(“Compound interest is “ + ci); } } 19 1)Save the file as CalculateInterest.java on desktop 2) Open command prompt and type cd desktop javac CalculateInterest.java java CalculateInterest
  • 20.
    Presented by NuzhatMemon Operators + Operators are special symbols used to build an expression. 20 Operators 1. Arithmetic Operators 2. Comparison operators 3. Logical Operators 4, Conditional operators 5, Assignment operator
  • 21.
    Presented by NuzhatMemon 21 Basic arithmetic operators are +, – , * , / , % (Modulus) All these operators are binary, they take 2 operands. Used for byte, short, int, long, float, double Arithmetic Operators Operator Meaning Eg + Addition 2+8 – Subtraction 2-8 * Multiplication 2*8 / Division 8/2 % Reminder 8%3 ++ is increment operator, which adds 1. -- is decrement operator, which subtracts 1. Increment/Decrement Operators ++x pre-increment x++ post-increment --x pre-decrement x-- post-decrement Comparison operators are known as relational operators. Comparison Operators Operator Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to Logical operators known as boolean operators. Logical operators : AND, OR, XOR (Exclusive OR), NOT Operator Sign Use AND && Used to combine two values – AND OR || Used to combine two values – OR XOR ^ It returns true only if operands are different NOT ! Operant is true, result is false & vice versa Also known as ternary operator. Conditional operator uses 3 operands. It uses two symbols ? and : in the expression. Syntax : (boolean-expr) ? (expr1) : (expr2) To assign a value to variable by using the assignment operator ‘=’ Syntax: <variable> = <expression>; Conditional Operator Operators Arithmetic Operator Increment and Decrement Operator Comparison Operator Logical Operator Assignment Operator
  • 22.
    Presented by NuzhatMemon Looping Java supports 3 types of looping constructs: (1) for loop + Entry controlled or pre-test loop constructs. + for loop are executed if condition is true. + It is used when numbers of iterations are pre-defined. (2) while loop + Entry controlled or pre-test loop constructs. + Loop are executed if condition is true. + It is used when number of iterations are not pre-determined. (3) do...while loop + Exit controlled or post-test loop constructs. + It repeats the loop if the test condition is true. + Here, statements of loop are executed at least once. 22
  • 23.
    Presented by NuzhatMemon Statement (1) IF STATEMENT + Used in a program to take one of two alternative coursed of action, depending upon Boolean valued expression. (2) SWITCH STATEMENT + Used when there are many alternative actions to be taken depending upon the value of a variable or expression. + Test expression should be of the type byte, char, short or int or enum 23
  • 24.
    Presented by NuzhatMemon Statement BREAK STATEMENT + Used to transfer the control outside switch/loop structure. + It is used to exit the loop. + It is jumps outside the nearest loop containing this statement. CONTINUE STATEMENT + It is used to skip the following statement in a loop and continue with the next iteration. 24
  • 25.
    Presented by NuzhatMemon Thanks! Any questions? You can find me at: + nuzhatmemon.com + Nuzhat.memon@gmail.com 25