Javalesson 1
Javalesson 1
Javalesson 1
Variables
The whole idea of programming is to store, manipulate, and output information or data.
Therefore it is only logical to be able to create and label places in which we can store that
data. These are called Variables and the Java language has different types of variables to
suit our different needs as programmers.
o int Stores integer values. That means that the can only be whole numbers and
cannot contain decimals. In Java some variables do have a size limit(See long
variable type)
Examples of creating an int Variable:
int age = 15;
int phone number = 8505532564;
int army_time = 2400;
Possible mistakes:
int age = 15.0; The programmer 15.0 and all numbers with a
decimals are double values.
Int age = 15; Int instead of int. Also the keyword Integer
relates to the Integer class in OOP so dont worry about it for
now. It will work, but you shouldnt use it yet.
o double used to store decimal values. The name double basically means that it
has information on 2 sides of the decimal point unlike ints. If you try to store an
int number inside a double, the value will automatically be converted into a
double (We will talk about variable type conversion later on)
Example of creating
double myMoney = 25.15;
double percentageOfJuniors = 0.55;
double averageAge = 17.72;
Possible mistakes:
double myMoney = 25.15 the line is missing a semicolon ; and
will give you an error.
Double myMoney = 25.15; similar to with ints, this will work,
but it is using OOP which we will learn later and you may run into
unexpected errors using it now.
o boolean used to store either true or false values. They are used when we employ
logic and decision making in our program.
Example of creating
boolean hungry = true;
boolean atSchool = true;
boolean homeworkIsDone = false;
Possible mistakes:
boolean hungry = false the line is missing a semicolon ; and
will give you an error.
Boolean hungry = true; This will work, but uses OOP (see
above).
o char similar to a String, but can only store single character. Must use single
quotations .
o long similar to int, but can store more information(much larger numbers)
o float similar to double, but can store less information(shorter decimals)
Print Statements
Print Statements Introduction
o As the name suggests, Print statements allow for the programmer to print out
information to the screen. This is usually done using variables, but we can print
information to the screen without storing it in a variable.
List of common Printing Statements:
o System.out.print();
This is the most common way to print information. Whatever is inside the
() parentheses will be printed to the screen.
This can be said in English as System dot out dot print. Associating the
code with words personally helped me to remember them.
Example that prints the number 15 to the screen:
int myAge = 15;
System.out.print(myAge);
o System.out.println();
This is exactly like System.out.print(); except when you use another print
statement after that it, the information will automatically be on the next
line
This can be said in English as System dot out dot print line with the
line part meaning that you skip a line afterwards.
o System.out.printf();
This statement is used to print information in a very specifically formatted
way, however I will not be covering this. If you are truly interested you
can ask me (Ibrahim) or one of our officers or look it up online. You
honestly probably wont need it.
Concatenation and combining Variables values
o Adding values
When printing, we can add values together to print them next to each
other. This is called Concatenation. I will not expect you to memorize the
name, just the concept.
If these values are numbers, then they will be mathematically added
together
String Literals: While we can simply store information to variable and
then print that, we can also print the raw values without creating a
variable. In the case of a String, it is called a String Literal in Java.
Examples:
Basic concatenation
o String bankAccount = Money I have in Dollars:
double myMoney = 10.0
System.out.print( bankAccount + myMoney)
o Prints Money I have in Dollars: 10.0
Number concatenation
o System.out.print(4 + 10.0)
o Prints 14.0
String Literals
o System.out.print(I like pie)
o Prints I like pie
String Literals with concatenation
o System.out.print(I like pie + and I like computers.)
o Prints I like pie and I like computers.
Java Lesson I Variables, Print statements
Review What you need to remember:
Variables
o VariableType VariableName = VariableValue;
o Types: String, char, int, long, double, float, boolean
(To be honest, we will mostly use Strings, ints, and doubles. The others
may show up occasionally depending on what we are trying to
accomplish)
Print Statements
o Difference between print() and println()
o Concatenation of data
Misc.
o Keywords: words reserved in Java have a special
meaning. You cannot give you variable the name int
without getting an error.
{
public static void main(String [] args) // This is the main method.
//See C2
{
System.out.print(Hello World!);
System.out.println(My name is + name + .);
System.out.println(I am + age + years old);
System.out.print(I earned a(n) + score);
System.out.println( on the last programming test);
System.out.println(It is good, but I can improve.);
}
}
Program Output:
Hello World! My name is John.
I am 15 years old
I earned a(n) 81.5 on the last programming test
It is good, but I can improve.
Extended Explanations
C1: Classes relate to OOP. We will discuss this later on. Your
program must be written within a public class. It must be named
Main (for now).
C2: Methods (also called functions) relate to OOP. All you code (for
now) must be written inside the main method. You will more or less
have to memorize public static void main(String [] args) , but you all
will use it frequently and get used to it.