Javalesson 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Programming Club: Java Lesson I Variables, Print statements, if-else Statements

Topics covered in this lesson:


Java Primitive Variable Types(String*, char, int, long, float, double, boolean)
Print Statements and concatenation( Println() vs Print() )
Basic logic( AND and OR operators, if-else Statements)
*Strings are technically not Primitive variables, and are actually more like an array of chars. They are fairly
easy to use and can be thought of as basically the same as Primitive Variables until we get into OOP (Object-
Oriented Programming)

IF YOU ONLY WISH TO REVIEW THE TOPICS GO TO PAGE 5

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.

How to make a Variable:


o VariableType Variable_Name = VariableValue;
The type is what kind of information is being held. They are CASE
SENITIVE.
The name is how you refer to it or use it in the program. They are CASE
SENITIVE.
The value is what will used in place of the variable if you use the
variables name.
The semicolon at the end is used mark the ending of a line/instruction.
o Example using Psuedo Code(Fake code used for learning concepts):
NumberVariable age = 18;
o It is extremely important to understand that the information held inside a variable
CAN BE CHANGED!!
o Example:
NumberVariable age = 18;
age = 20;
From this point onward in the program, the value inside the age variable
will be 20 instead of 18.

List of the primary Primitive Variable types:


o String Stores words, letters sentences and just about anything you could write
with text. The data/value must be contained within quotation marks. The can
include numbers and symbols, but they will NOT have the mathematical value of
the number.
Examples of creating a String Variable:
String myName = John;
String address = 1010 Oak Tree Lane;
String email = [email protected];
Possible mistakes:
String name = John; The programmer did not use double
quotations instead they used single quotations.
string name = John; The programmer did not capitalize the S
String. (Explain the idea of keywords in a language if necessary)

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.

o Object Oriented Programming (OOP): OOP is a


structural design we implement to reuse code we
wrote earlier in a program. It also allows us to use
prewritten code that the creators of Java already made
to help us do things such as complex math operations
or putting pictures on the screen. This topic will be
discussed in-depth on a later date.
Your First Program:
//This is a comment
//It will not affect the program in any way. Your program will run with or
without them.
//It helps other programmers reading your code to understand your reasoning.

public class Main // This is the Main class. See C1

{
public static void main(String [] args) // This is the main method.
//See C2
{

int age = 15;


String name = John;
double score = 81.5;

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.

You might also like