The Basics Input Output and Primitive Data Types

The Basics Input / Output, and Primitive Data Types

System. out • System. out has two methods you can use for output: . println and. print • The difference is where the carriage goes when the output is finished. • . print leaves the carriage at the end of the line whereas. println drops the carriage to the beginning of the next line. • All typing between the (“ “) is called a literal String and is displayed on the console.

Escape Sequences • Escape sequences are done inside a literal String. • There are 3 escape sequences to help you make your text more aesthetically pleasing: • n returns the carriage to the next line. • ” makes a literal quotation. • \ makes a literal backslash.

Input • You are not expected to do any inputting on the A. P. test. • If the testers want you to use any input, they will declare something like: String user. Input = io. readln; * We will use input and I will show you how later.

Variables • Variables are vessels for holding data, think of them as a bucket. • Variables names must start with a letter, and by convention should start with a lower-case. • If a variable is more than one word, there should be no spaces with each new word capitalized. • For instance: my. New. Variable.

Variable Types • There are 3 primitive data types that we will start with: int, double, and boolean. • These are like three different types of buckets that can hold 3 different types of things. • int variables can hold integer values. • double variables can hold rational values. • boolean variables can only hold true or false

Variable Declaration • Variables are declared by giving the variable a type (telling it which bucket it is). • int number; • double decimal; • boolean is. An. Awesome. Guy;

Variable Assignment • Assignment is done using the = sign: • number = 52; is. An. Awesome. Guy = true; • Assignment is done left to right so: 25 = number; is not a valid statement. • Variables may be declared and assigned in the same statement: int my. New. Num = 10;

Changing a value • Variables are so named because they can change value. • To reassign a value, simply use the = sign again. • double some. Num = 5. 3; some. Num = 2. 4; * Notice the type was not given again- once a variable is declared it may never change (bucket) types so it is redundant to redeclare.

Your Turn! • Define a class that is your name, and declare a main method inside of it(leave about 6 lines of space for code. • Output a line that says: Hello and then returns the cursor to the next line. • Output in one statement something that says: I am so darn “Happy” to be here!

Your Turn cont. • Declare 3 variables called integer. Number, decimal. Number, and boolean. Value with the values: -1025, 27. 384, and false respectively. Output the values. • Change integer. Number to 0 and boolean. Value to true. Output the values.
- Slides: 11