Download 1 Objective: SWBAT explain how to define and use “primitive

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Object-oriented programming wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Class (computer programming) wikipedia , lookup

Scala (programming language) wikipedia , lookup

Java syntax wikipedia , lookup

Name mangling wikipedia , lookup

Corecursion wikipedia , lookup

Go (programming language) wikipedia , lookup

Java (programming language) wikipedia , lookup

C syntax wikipedia , lookup

Java performance wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Name: _________________________________________
Per: ___
Date: _______________
APCS Lesson 1-1: Variable Types and Identifiers
Objective: SWBAT explain how to define and use “primitive variables” in the Java programming
language.
Types & Identifiers: (“Setting up the primitive variables”)
Computers need to store and keep track of various types of information in all computer
programs. We have to define EVERY storage location with the type/size of the data and give
it a variable name. Here are sample Java statements for the types of variables we will use:
________________________________________ // “true” or “false” (reserved Java words)
________________________________________ // integer values
________________________________________ // BIG integer values (not on the AP Exam)
________________________________________ // decimal values
________________________________________ // basic strings for printing, lots more later…
Things to be careful about:
1. Java is VERY type-sensitive, so understand the data that will be placed in each variable.
2. If your data is too big for your variable, then Java will either ________________ it or throw
an ____________________________ (nasty computer error message). Both may be bad!
3. Decimal values are only very precise approximations in computer memory. More later …
Converting Types: (“Casting”)
Sometimes, we need to use multiple variables of different types, so we need to convert (or
“cast”) some of them to a different type. Some examples of “casting” numeric values are:
int int1 = 4, int2 = 7, total;
double doub1 = 11.6, double2 = 13.4, result;
___________________________________ // casting an integer to a decimal (explicitly)
___________________________________ // casting an integer to a decimal (automatically)
___________________________________ // casting a decimal to an integer
___________________________________ // compilation error! Possible loss of data …
Java always ___________________ values when converting decimals to integers! Use
parenthesis when converting multiple decimal values to control when this occurs.
int result1 = ________________________________________________________
Java assumes all values are “double” if ______________________________. Otherwise, it
only converts to the decimal after all calculations have been completed.
int int1 = 17; int 2 = 5;
double result2 = double (int1 / int2)
// results2 = __________________________________
double result2 = double int1 / int2
// results2 = __________________________________
Special String Characters:
“/” is a special “control character” used to preceded other characters to control output
formatting. The most important of these is “/n” which prints a new line. To output a “/”, we have
to use “//”. Formatting data values is not required for your AP Computer Science Exam.
Our First Java Application:
Let’s complete the code below together to write our very first Java application to combine a few
variables and output a String to our computer terminal.
/*
* This is a comment at the top of our class. We’ll get more formal about this later …
*/
public class TestProgram { // TestProgram is the name of our main test driver class here
public static void main { // required method in the main driver class in all applications
String str1 = “Hello World! My name is “; // Here is a small string …
String str2 = “____________________ “;
int myAge = _______ ;
// another string …
// An integer value …
System.out.println(str1 + str2 + “. My age is “ + myAge + “.”);
}
}
// end public static void main method
// end public class TestProgram
When you have Java and your development environment installed on your home computer, you
can type in this program, compile it, and execute it to see the results! Be careful with required
Java keywords like “public” and “class” – capitalization and spelling is critical with required
keywords. You may use as many blanks as you like in the statements – space things out to
make your application readable. You should indent parts of the program and don’t forget to line
up the closing braces and to add comments. Also, notice there are two different formats for
commenting your applications. The first method is generally used before classes and important
methods; the second method is generally used within classes or methods. We’ll talk more
about the first method when we discuss generating “javadocs” documentation for our
applications. For now, you can use either format in your applications.
2