introduction to java thanks to dan lunney (shs). java basics file names the “main” method output...

Post on 12-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to JavaIntroduction to Java

Thanks toThanks toDan Lunney (SHS)Dan Lunney (SHS)

Java BasicsJava Basics

• File names

• The “main” method

• Output to screen

• Escape Sequence – Special Characters

• format() method

• Conventions

File NamesFile Names

• In JCreator the file name and class name must be identical

• Always start class names with a capital letter• If more than one word in file name all names are

capitalized • Always begin java programs with:

public class FileName {Code goes here…

}

The “main” methodThe “main” method

• Executes the main section of code

• All other methods “called” from here

• Syntax for main method is:public static void main(String[] args) { code in here…}

Output to ScreenOutput to Screen

• To print to screen we use the line: System.out.println();

• What ever is in the () will print• Text must be in “” marks• There are two options

– println – prints a line and the enters to start new line– print – prints line and remains on same line

• Example: System.out.println(“Hello class”); prints Hello class on the screen

Escape Sequence – Special Escape Sequence – Special CharactersCharacters

• By including special characters within the quote marks, java will perform special formatting

• \n – new line• \t – tab (8 spaces)• \\ - backslash• \” – double quote mark• Example: System.out.print(“Hello\t”);

System.out.print(“class”);prints Hello class

format() methodformat() method

• Can format the look of text• Example: • System.out.format(“%-10s %-8s”, “Hello”, “class”);• Prints – Hello class (5 spaces between)

• Symbols– % - indicates start of format– “-” indicates left align (nothing for right align)– # indicates how many spaces for text– S indicates it’s a string

Java ConventionsJava Conventions

• Always use in an introductory comment with name, date, and project

• All class names start with Upper case letters• Comments should be included before all

methods to explain them• All code statements should be indented from

method line• Place starting curly brace { on the same line as

classes and methods. Place the ending curly brace } on its own line with nothing else.

top related