1 of 65

AP Computer Science

Java Basics & Output

Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

This work is licensed under an Attribution-NonCommercial-ShareAlike 3.0 Unported License

2 of 65

Hello World Recap

3 of 65

4 of 65

Hello.java

public class Hello

{

}

  • The keyword class announces a new class is about to be named
  • Hello is the name of the class
  • The braces mark the beginning and end of class Hello
  • For each open brace there must be a corresponding closing brace.

5 of 65

6 of 65

Hello.java

  • The previous empty class will compile, but it will not run without a starting point
  • The starting point for any Java program is a main() method
  • The main() method is required as shown below.

public class Hello

{

public static void main(String[] args)

{

}

}

7 of 65

8 of 65

Hello.java

  • The previous program will run, but it will not do anything
  • Now, we add a print statement to our main()

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world!");

}

}

9 of 65

10 of 65

Full Hello.java

  • Remember everything is in a class
  • The class name must match the file name (Hello.java)
  • The main()method is where the program starts
  • The print statement outputs information on the screen

public class Hello

{

public static void main(String[] args)

{

System.out.println("Hello world!");

}

}

11 of 65

Output Statements

12 of 65

13 of 65

Output Statements

  • Basic output is done with print statements
  • You put what you want to print inside the parentheses

  • Print statements are a great tool to use when troubleshooting a problem!

14 of 65

15 of 65

Output Statements

You can print any ONE thing

  • Any text enclosed in quotes

System.out.println("43 eggplants");

  • Any number

System.out.println(3.14159);

16 of 65

17 of 65

println()

  • You can use System.out.println()to display each statement on a separate line

System.out.println("Hello");

System.out.println("World");

Output

Hello

World

  • Starts printing wherever the last print or println left off. After everything between the quotes is printed, the println finishes by printing a newline (i.e. it goes to the beginning of the next line of output)

18 of 65

19 of 65

print()

  • You can use System.out.print()if you do not want the text on different lines

System.out.print("Hello");

System.out.print("World");

Output

HelloWorld

  • Starts printing wherever the last print or println left off and prints everything between the quotes. The next print or println will start immediately after the last character before the closing quote.

20 of 65

21 of 65

Output Statements

To print MULTIPLE things you must join them together into ONE thing using the + operator.

  • System.out.println("pi="+3.14159);

NOTE: When there is a string on the left or right hand-side of the + operator JAVA automatically converts whatever is on the other side into a string and concatenates (i.e. joins together) the two strings.

22 of 65

Java Language Basics

23 of 65

24 of 65

Java Syntax & Semantics

  • Syntax defines the structure of the program
    • Like English, Java has a strict set of rules you use when creating a program

  • Semantics defines the meaning of a program
    • A program can be syntactically correct, but does not make sense or contain logic errors

Wikipedia entry discussing differences

25 of 65

26 of 65

Classes

  • In Java, one of the most basic rules of syntax is everything must be inside a class

  • For now, think of a class as a way to organize your code

27 of 65

28 of 65

Classes

  • Let us say we are going to create a Java program named Dogs
  • We create a file named Dogs.java and put a class named Dogs inside of it
  • Do you believe Dogs is the same as dogs?
    • No JAVA, like most programming languages, is case-sensitive (Dogs and dogs are different identifiers).

29 of 65

30 of 65

Class Naming Conventions

The name for something, like a class, is called an identifier.

  • An identifier consists of alphanumeric characters
  • For a class the convention is to capitalize the first character (and it must be alphabetic - i.e. class name can't start with a number)
  • The first character of each following word should also be capitalized
  • The remaining characters can be mixed alphanumeric characters
  • No spaces are allowed inside the name

31 of 65

32 of 65

Class Naming Conventions

Which of the following look like good identifiers for a class name?

  1. ExcitingGame
  2. Lady Luck
  3. x32
  4. lastChance
  5. x/y
  6. HelloWorld
  7. 2For1

33 of 65

34 of 65

Semicolons

  • In Java we separate different statements with a semicolon ( ; )
  • If we want to do a number of statements, we type them in order, with a semicolon after each one

35 of 65

36 of 65

Braces

  • Braces are an important part of Java syntax
  • For every { you need a corresponding }
    • It is a good habit to add both at the same time
  • A brace indicates either the start or end of a block of Java code
  • Never put a ; before an open {

37 of 65

38 of 65

Case Matters

  • Java is a case sensitive language
  • Class is not the same as class
  • System.out.println("Word!"); prints correctly
  • system.Out.Println("Word!"); does not compile

39 of 65

40 of 65

Reserved Words

  • Reserved words are a set of words in Java with special meanings
  • These are words used in Java for specific purposes
  • You cannot use these words when creating your identifiers
  • Some examples are: public, class, void, and static

41 of 65

42 of 65

Sequencing

  • For example, instead of one print statement, we can have several:

  • Each statement is an instruction to the computer
  • They are printed in order, one by one

System.out.println("Hello, world!");

System.out.println("Hello, galaxy!");

System.out.println("Goodbye, world!");

43 of 65

44 of 65

Whitespace

  • Java generally ignores whitespace (tabs, newlines, and spaces)

System.out.println("Hello, world!");

System.out.println( "Hello, world!");

is the same as:

  • You should use white space effectively to make your code readable

45 of 65

46 of 65

Comments

  • When looking at a program, it can be confusing to see what is taking place
  • To help someone who looks at your code understand you can leave a summary of what is taking place in a comment
  • Although comments appear in the code, they are not executed
  • Comments are also a great tool to use when you are troubleshooting a problem!

47 of 65

48 of 65

Comments

  • There are two kinds of comments we will use

  • Single line comments use //

System.out.println("Hi!"); // this is a comment

  • This type of comment is great for adding a quick note, or for commenting out a line of code with an error in it

49 of 65

50 of 65

Comments

  • Multi-line comments start with a /* and end with a */
  • These should be used when you want to add a more descriptive comment or if you have a larger block of code causing an error

/* this is really long,descriptive,definitive,

detailed,eloquent,expressive,identifying,

illuminating,revealing multi-line comment */

System.out.println("Hi!");

51 of 65

Javadoc Comments

  • Multi-line comments that start with a /** and end with a */ that are placed before class, attributes (aka instance variables), or method declarations.
  • The HTML file generated by Javadoc will describe each attribute and method of a class.
  • Within a Javadoc comment, the @param and @return tags can be used to describe a method's parameters and return value. The @param tag should be followed by the parameter's name, and then a description of that parameter. The @return tag is followed simply by a description of the return value.

52 of 65

53 of 65

Javadoc Comments

  • Javadoc is a tool provided with the JDK to help document large programs.
  • Javadoc creates an HTML file that describes the attributes and methods of a class.
  • Javadoc comments start with a /** and end with a */ and are placed before class, attributes (aka instance variables), or method declarations.
  • Within a Javadoc comment, special tags (@tag) can be used to describe a method's parameters and return value.
    • The @param tag should be followed by the parameter's name, and then a description of that parameter.
    • The @return tag is followed simply by a description of the return value.

54 of 65

55 of 65

Javadoc Comments

/** equals method to determine if two songs are equal

* @param obj which is a reference to

the song object to be compared

* @return return true or false if the song is equal*/

public boolean equals(Object obj) {

// leave this here

// check equality using the s reference

Song s = (Song) obj;

if ((title == s.getTitle()) && (album == s.getAlbum()))

return true;

else

return false;

}

56 of 65

Escape Sequences

57 of 65

58 of 65

Escape Sequences

  • Escape sequences are special combinations of a \ followed by another character
    • Think about what you know of text already
    • In a print statement we use " " to display text
    • What if you wanted "Hello World" to be displayed with the quotes?
    • Would you say ""Hello World""?

59 of 65

60 of 65

Common Escape Sequences

Escape Sequences

\t

Inserts a tab in the text at this point.

\n

Inserts a newline in the text at this point.

\"

Inserts a quote in the text at this point.

\'

Inserts an apostrophe in the text at this point.

\\

Results in a single \ being displayed

61 of 65

62 of 65

Escape Sequences Examples

Escape Sequences Examples

System.out.println("Hello\tWorld")

Hello World

System.out.println("Hello\nWorld")

Hello

World

System.out.println("\"Hello World\" ")

"Hello World"

System.out.println("\'Hello World\' ")

'Hello World'

System.out.println("Hello\\World")

Hello\World

System.out.println("Hello\t\\\"World")

Hello \"World

63 of 65

E

ASCII Art Examples

64 of 65

E

ASCII Art Examples

65 of 65