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
Hello World Recap
Hello.java
public class Hello
{
}
Hello.java
public class Hello
{
public static void main(String[] args)
{
}
}
Hello.java
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
Full Hello.java
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
Output Statements
Output Statements
Output Statements
You can print any ONE thing
System.out.println("43 eggplants");
System.out.println(3.14159);
println()
System.out.println("Hello");
System.out.println("World");
Output |
Hello World |
print()
System.out.print("Hello");
System.out.print("World");
Output |
HelloWorld |
Output Statements
To print MULTIPLE things you must join them together into ONE thing using the + operator.
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.
Java Language Basics
Java Syntax & Semantics
Classes
Classes
Class Naming Conventions
The name for something, like a class, is called an identifier.
Class Naming Conventions
Which of the following look like good identifiers for a class name?
Semicolons
Braces
Case Matters
Reserved Words
Sequencing
System.out.println("Hello, world!");
System.out.println("Hello, galaxy!");
System.out.println("Goodbye, world!");
Whitespace
System.out.println("Hello, world!");
System.out.println( "Hello, world!");
is the same as:
Comments
Comments
System.out.println("Hi!"); // this is a comment
Comments
/* this is really long,descriptive,definitive,
detailed,eloquent,expressive,identifying,
illuminating,revealing multi-line comment */
System.out.println("Hi!");
Javadoc Comments
Javadoc Comments
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;
}
Escape Sequences
Escape Sequences
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 |
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 |
E
ASCII Art Examples�
E
ASCII Art Examples�