Part 1 : Variables
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
April 14, 2025
We’ve already familiarized ourselves with strings to a degree while dealing with user inputs.
Let’s turn our attention to learning about other variable types commonly used in Java.
A variable can be thought of as a container in which information of a given type can be stored.
Examples of these different types include text (String
), whole numbers (int
), floating-point numbers (double
), and whether something is true or false (boolean
).
A value is assigned to a variable using the equals sign (=
).
In the statement above, the value of 12 is assigned to an integer variable called months
. The statement could be read as: “the variable months is assigned the value 12”.
A variable’s value can be joined to a string using the +
sign, as seen in the following example:
String text = "contains text";
int wholeNumber = 123;
double floatingPoint = 3.141592653;
boolean trueOrFalse = true;
System.out.println("Text variable: " + text);
System.out.println("Integer variable: " + wholeNumber);
System.out.println("Floating-point variable: " + floatingPoint);
System.out.println("Boolean: " + trueOrFalse);
The Code template tab contains a program that prints the output shown in the Sample Output tab.
Modify the program in the right places so that it prints the output shown in the Modified output tab.
public class VariousVariables {
public static void main(String[] args) {
// MODIFY THESE:
int numberOfChicken = 3;
double baconWeight = 5.5;
String tractor = "None!";
// DON'T MODIFY THESE:
System.out.println("Chicken:");
System.out.println(numberOfChicken);
System.out.println("Bacon (kg):");
System.out.println(baconWeight);
System.out.println("Tractor:");
System.out.println(tractor);
System.out.println("");
System.out.println("And finally, a summary:");
System.out.println(numberOfChicken);
System.out.println(baconWeight);
System.out.println(tractor);
}
}
pi
twice.Sample output
int number = 123;
System.out.println("The value of the variable is " + number);
number = 42;
System.out.println("The value of the variable is " + number);
Output
Let’s look at the preceding program’s execution step-by-step.
int
) and its name (in this case number
), the computer creates a ‘named container’ for the variable.number
variable – its value is retrieved from a container that has the corresponding name.number = 42
), a check is run to see whether a container with the given name already exists.number
variable.number
from a container having its name.boolean integerAssignmentWillWork = false;
integerAssignmentWillWork = 42; // Does not work
int value = 10;
integerAssignmentWillWork = value; // Neither does this
Variables Express the Program and the Problem to Be Solved
Programming is a problem-solving tool.
The aim is to create solutions for any given problem, such as the automation of control in cars.
As a problem is approached, the developer decides on the terms used to describe the problem domain.
The terminology that is chosen, such as variable names, will serve to describe the problem for anyone who is to work with it in the future.
As you’re wording the problem that you’re solving, think about the concepts related to that problem and appropriate terms that could be used to describe them.
If you find it hard to come up with relevant names, think of the ones that definitely do not describe it.
After this, settle on some terminology that you’re going to use – the good thing is that you can usually improve on it later on.
Variable naming is limited by certain constraints.
int 7variable = 4; // Not allowed!
int variable7 = 4; // Allowed, but is not a descriptive variable name
System.out.print
and System.out.println
.Permissible Variable Names
lastDayOfMonth = 20
firstYear = 1952
name = "Essi"
Impermissible Variable Names
last day of month = 20
1day = 1952
beware! = 1910
*1920 = 1
A variable’s type is specified when it’s initally declared.
String string = "text";
, and an integer having the value 42 is declared with the statement int value = 42;
.A variable’s type determines the types of values that can be assigned to it. String
types hold text, int
types whole numbers, double
floating-point numbers, and boolean
types are either true or false.
As such, the possible values of a given variable type are limited. For example, a string cannot contain an integer, nor can a double contain a boolean value.
Type | Example | Accepted values |
---|---|---|
Whole number, i.e., int |
int value = 4; |
An integer can contain whole numbers whose values lie between -2147483648 and 2147483647. |
Floating-point number, i.e., double |
double value = 4.2; |
Floating-point numbers contain decimal numbers, with the greatest possible value being approximately 21023. When a decimal number is represented with a floating-point number, the value can be inaccurate as floating-points are incapable of representing all decimal numbers. |
String |
String text = "Hi!"; |
A string can contain text. Strings are enclosed in quotation marks. |
True or false value, i.e., boolean |
boolean right = true; |
A boolean contains either the value true or false . |
nextLine
-command of the Scanner
helper method.Integer.valueOf
command converts a string to an integer. It takes the string containing the value to be converted as a parameter.Output
Scanner
object, the reading of the value is usually inserted directly into the type conversion. This happens like so:import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Write a value ");
int value = Integer.valueOf(scanner.nextLine());
System.out.println("You wrote " + value);
}
}
Output
Write a program that asks the user for a value and then prints the value provided by the user.
Double.valueOf
command converts a string to a double. It takes the string containing the value to be converted as a parameter.String valueAsString = "42.42";
double value = Double.valueOf(valueAsString);
System.out.println(value);
Output
Write a program that asks the user for a floating-point number using the variable type Double. The program then prints the user’s input value.
Integer.valueOf
command converts a string to an integer and the Double.valueOf
converts it to a floating-point.valueOf
command also appears when converting a string to a boolean – this is done with the command Boolean.valueOf
.true
or false
.true
.true
.false
.Write a program that asks the user for a boolean value. The program should then print the value provided by the user.
Example print 1
Example print 2
Finally, a summary:
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
int integer = Integer.valueOf(scanner.nextLine());
double floatingPoint = Double.valueOf(scanner.nextLine());
boolean trueOrFalse = Boolean.valueOf(scanner.nextLine());
// and so on
}
}
Write a program that asks the user for a string, an integer, a floating-point number, and a boolean. The program should then print the values given by the user.
Computer Language