Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23
Chapter 5: Java Basics
Class and main Method Headings
• Class: is simply a container for the program code • Class heading: public class Hello • public: class is accessible by other classes or programs • class: identifies the beginning of class code • Hello: class name. (class name describes the task of class)
• main Method: is where computer start execution of code
• main Method Heading: public static void main(String[] args) • public: everyone can access the method • static: method is accessible from any class • void: method doesn’t return a value • main: reserved name (keyword) • String[] args: arguments for main method Class and main Method Headings • Braces { } : • Opening brace { determines the beginning of a class or a method • Closing brace } determines the end of a class or a method Comments • One-line: // • Block-comment: /* */ Example: /* This program prints Hello World! On screen It is an example of using comments with codes*/ public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); //print on screen } } Readability Increase the program readability by: • Comments • Blank lines: makes the program easy to read and understand Hard to understand Easy to understand public class Hello{public static void public class Hello main(String[] args) { {System.out.println("Hello World! ");}} public static void main(String[] args) { System.out.println("Hello World! "); } } Getting Inputs and Outputs • Scanner stdIn = new Scanner(System.in): takes inputs from keyboard • stdIn is the name of the scanner • Note: the statement "import java.util.Scanner;" should be used to use scanner. • Examples: • int x = stdIn.nextInt() • float y = stdIn.nextFloat() • String s = stdIn.nextLine() • System.out.println( ) statement gives output on screen • Examples: • System.out.println("Hello World! "); • System.out.println("I am " + "a programmer"); • System.out.println(result); Keywords • Keywords: are words that have already been defined for Java compiler. They have special meaning for the compiler and cannot be used as a variable, class or a method name. • Examples: • public,static,class,import (keywords for defining parts of code) • int, float, double, char (keywords for data types) • if, else, switch (keywords for conditions) • for, while (keywords for loops) Identifiers • Identifier: a name of any component in the program (a class, method, or variable) Examples: Hello , main, x, a2. • Letters (A-Z) , numbers (0-9), _ , $ can be used (use any combination) • Don’t start with a number • Cannot use keywords as names • Recommendation: • Identifier should be descriptive • class name should start with uppercase e.g. Hello • method name should start with lowercase e.g. printResult Variables • Variable: is a reserved memory location to store a value • Variable Datatypes: int, long, float, char, double, String • Variable can only hold one type of value. • Declaration of variables: writing data type and name in a single command • int a1; • float dist; • double result; • char op; • String msg; • Assignment: writing variable name, = , and value in a single command • a1 = 25; • Op = ‘+’; Variables • Initialization: writing data type, name, and value in a single command • int a1 = 64; • float dist = 31.7f; • char op =‘%’; • String msg = "I am new to programming"; • Constant: • final int SPEEDOFSOUND = 343; • Recommendation: • variable name should start with lowercase e.g. firstName Variables and Type Casting • Type casting: means to convert the value from one data type to another • Conversion from smaller size data types to larger size data type are done automatically, but some precision could be lost. • Use the cast operator (int), (float), (double) to convert between different data types. • Examples • From float to integer: int x = (int) 5.25 • From integer to double: double r = (double) 41/3 Arithmetic Operations • Addition: a + b • Subtraction: x – y • Multiplication: d * r • Division: s / h • Modulus: k % q • Operator precedence order: • Parenthesis: () • Exponent: XY • Multiplication and Division: *,/ • Addition and Subtraction: +,- Relational Operations • Greater than : a > 6 • Less than: x < 15 • Greater than or equal: c >= d • Less than or equal: p <= r • Equal: s == 10 • Not equal: h != 1 Escape Sequences • Escape sequences: are special characters used with strings. Escape Sequences • Example 1: System.out.println("Hello \nWorld"); Output is: Hello World • Example 2: System.out.println("Hello \tWorld"); Output is: Hello World Tracing • Tracing: means to understand the program details and follow the changes in values after each line of code. Program 1: Prints “Hello World” //This program prints the message "Hello World" on screen public class Hello { public static void main(String []args) { System.out.println("Hello World"); } } Program 2: Prints Values of Variables //This program prints the values of variables x and y on screen public class Variable { public static void main(String []args) { int x; // variable declaration x= 12; // value assignment to a variable float y = 6.89f; //variable initialization System.out.println("x="+ x); System.out.println("y="+ y); } } Program 3: Getting Inputs from keyboard //This program gets the values of variables x and y from keyboard import java.util.Scanner; public class Variable { public static void main(String []args) { Scanner input = new Scanner(System.in); //use scanner class for inputs int x; x= input.nextInt(); //gets integer inputs from keyboard float y = input.nextFloat(); //gets float inputs from keyboard System.out.println("x="+ x); System.out.println("y="+ y); } } Program 4: Constants (final) //This program declare a variable as a constant public class Variable { public static void main(String []args) { final int x= 92; // x=25; /*this will give an error because the value of the final variable cannot be changed*/ System.out.println(x+1); } } Program 5: Arithmetic Operations //This program adds two numbers public class Add { public static void main(String []args) { int a,b; a=9; b=8; System.out.println(a+b); } } Program 6: Arithmetic Operations //This program adds any two numbers entered by user import java.util.Scanner; public class Add2 { public static void main(String []args) { Scanner input = new Scanner(System.in); int a,b; a=input.nextInt(); b=input.nextInt(); int result=a+b; System.out.println("a+b=" + result); } } Program 7: Type Casting //This program demonstrates the use of type casting public class TCasting { public static void main(String []args) { int a,b; a=7; b=2; int idivision = a/b; float fdivision= (float) a/b; //to get the floating point System.out.println("integer division=" + idivision); System.out.println("float division " + fdivision); } }