1 / 23

Chapter 2: Java Fundamentals

Chapter 2: Java Fundamentals. Spring 2006-2007 Lory Al Moakar. 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

Download Presentation

Chapter 2: Java Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Java Fundamentals Spring 2006-2007 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

  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 • You should get 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 • 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, 965747

  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); } }

More Related