Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Java Fundamentals

Similar presentations


Presentation on theme: "Chapter 2: Java Fundamentals"— Presentation transcript:

1 Chapter 2: Java Fundamentals
Spring Lory Al Moakar

2 Outline 2.1 The Parts of a Java Program
2.2 The print and println Methods, and the Java Standard Class Library 2.3 Variables and Literals 2.4 Primitive Data Types 2.5 Arithmetic Operators 2.6 Combined Assignment Operators 2.7 Conversion Between Primitive Types 2.8 Creating Named Constants with final 2.9 The String Class 2.10 Scope 2.11 Comments 2.12 Programming Style 2.13 Reading Keyboard Input 2.14 Dialog Boxes 2.15 Common Errors to Avoid

3 2.1 The Parts of a Java Program
Comment for the reader // This is a simple Java program. public class Simple { public static void main(String[] args) System.out.println("Programming is great fun!"); } Name of the class (program) Main part of the program

4 Comments Are ignored by the compiler
Are used to clarify the purpose of the program or line of code

5 Class Every program in Java has to be a class
Always a class has a name( Simple in this example ) Your file has to be named this name followed by .java ( Simple.java is the name of this file ) Important: Java is case sensitive so pay attention to the letters and the case they are in. (ex: simple is different from Simple)

6 Main part of the program
Usually it starts with a method header: public static void main ( String args[] ) After the { you write the commands that you want the program to execute Every { you open it should be closed } at some point in your program Every line in the main part of the program has to end in ;

7 Main part of the program
System.out.println("Programming is great fun!"); This statement prints Programming is great fun! It prints whatever you put between “ and “

8 Special characters ; Semicolon Ends a complete statement //
Double slash Beginning of a comment ( ) Opening and closing parentheses Used in method headers { } Opening and closing braces Encloses a group of statements “ “ Quotation marks Encloses a string of characters ; Semicolon Ends a complete statement

9 print and println System.out.print or System.out.println
System.out.print displays the text between “ “ exactly as it is System.out.println same as System.out.print except that after it displays the text on the screen it move the cursor to the beginning of the next line

10 Exercise in class download the tool DrJava
Write the following in DrJava public class printTest { public static void main (String args[]) { System.out.print(“Hello” ); System.out.print(“There”); }

11 Exercise in class Save it as PrintTest.java Compile and run
You should get Hello There Now change print to println Save, compile and run Hello There

12 Escape Sequences Allow you to control the way output is displayed
Are characters that have a special meaning when put within a String Literal Example: System.out.print( “Hi \n CS7” ); Displays: Hi CS7

13 Escape Sequences \n \t \b
Moves the cursor to \n a new line \t the next tab stop \b one character back \r To the beginning of the line \\ Prints a backslash \’ Prints a single quote \” Prints a double quotation mark An escape sequence starts with a backslash and is followed by a control character Here is a list:

14 Example // Another well adjusted printing program public class Tabs {
public static void main(String[] args) System.out.print("These are our top sellers:\n"); System.out.print("\tComputer games\n\tCoffee\n "); System.out.println("\tAspirin"); } Output: These are our top sellers: Computer games Coffee Aspirin

15 2.3 Variables and Literals
Variables allow you to store and work with data in the computer memory Each variable has a type and a name

16 Variables A variable has to be declared, and initialized
Variable Declaration: type identifier; Variable Initialization: identifier = literal;

17 Literals String Literal is enclosed between quotation marks ex: “This is me” Integer Literal is not enclosed in quotation marks and has only numbers ex: 1,53,

18 Identifiers An identifier is a programmer-defined name that represents some element of the program. Ex: variable names & class names

19 Rules to follow when naming identifiers
The first character must be one of the letters a-z, A-Z, an underscore, or a dollar sign After the first character, you may use one of the letters a-z, A-Z, an underscore, a dollar sign or a digit 0-9 Uppercase and lowercase characters are different Identifiers cannot include spaces, points, commas or punctuation An identifier cannot be one of the keywords

20 Keywords abstract default if private this
boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized

21 Good practices when naming identifiers
Good descriptive names Use uppercase letters when having two or more words to form an identifier Do not use single lettered identifiers

22 Printing a variable 2 ways: On its own:
System.out.println( identifier); Combined with text: System.out.println( “The result is: “+ identifier );

23 Example // This program has a variable. public class Variable {
public static void main(String[] args) int value; value = 5; System.out.print("The value is "); System.out.println(value); }


Download ppt "Chapter 2: Java Fundamentals"

Similar presentations


Ads by Google