Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]

Similar presentations


Presentation on theme: "A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]"— Presentation transcript:

1 A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[] args){ System.out.println( i ); int i = 20; int i = 20;}}

2 A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { int lenSquare = 4; areaSquare = lenSquare * lenSquare; areaSquare = lenSquare * lenSquare; System.out.println( areaSquare ); System.out.println( areaSquare ); }}

3 A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { int x = 2; int y = 3; int z = x%3; System.out.println(“z"); System.out.println(“z"); }}

4 A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[] args) { int lenSquare = 4; int areaSquare = lenSquare * lenSquare; System.out.println(“area of a square is: “+areaSquare+” cm”); System.out.println(“area of a square is: “+areaSquare+” cm”); }}

5 Variable Declaration  Name  Data type  Initialize with value double radius = 2.0;

6 Screen Output Using System.out.print/println int radius = 5; System.out.println(“radius”); Output: radius System.out.println(radius); Output: 5 System.out.println(“radius: “+radius); Output: radius: 5

7 Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 New line \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \ ' \u0027 Double Quote \ " \u0022

8 More Printing with System.out.println(); Todayissunny System.out.println(“Today\nis\nsunny”); NameIDAddress

9 Getting Input Using Scanner Scanner myScanner = new Scanner(System.in);  Scanner: pre-defined Java program (class object) to read in text input  myScanner: variable name  new Scanner(System.in) : new instance of the class object containing a reference to the default input of the system (keyboard)

10 Scanner Methods for Input 1. Create a Scanner object Scanner myScanner = new Scanner(System.in); 2. Use methods myScanner.next() => read in a string and put it in String.nextByte()=>read in a number put it in byte.nextShort() =>read in a number put it in short.nextInt()=>read in a number put it in int.nextLong() =>read in a number put it in long.nextFloat() =>read in a number put it in float.nextDouble() =>read in a number put it in double.nextBoolean() =>read in a string and convert to boolean **By default, Scanner input is delimited by white space characters (space, tab, carriage return)

11 Getting Input Using Scanner import java.util.Scanner; public class My1stJava { public static void main (String args[]) { Scanner myScanner = new Scanner(System.in); System.out.println("please enter text:"); String str = myScanner.next(); System.out.println(str); } Input: Hello World Output ?

12 The String Class ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ lV“Hello”

13 Constructing Strings String message = new String(stringLiteral); String message = new String(); //empty String Since strings are used frequently, Java provides a shorthand initializer for creating a string: String message = “ Welcome to Java ” ;

14 Strings Are Immutable A String object is immutable; its contents cannot be changed. String message = "Java"; String message = "Java"; message = "HTML"; message = "HTML"; In the 2 nd statement, Java actually created a new string container of type String to hold “ HTML ” and s now represent this new string container

15 Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7 )


Download ppt "A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]"

Similar presentations


Ads by Google