-
Notifications
You must be signed in to change notification settings - Fork 1
Java Basics
This week we will learn the basics of Java, a programming language. This guide will walk you through how to write your first Java program as well as how to compile and run your code.
- Printing on the Screen
- Mathematical Expressions
- Storing Values in Variables
- String concatiation
- Input/Output
System.out.println() is a Java statement that allows us to display messages on screen.
For example:
> public class HelloWorld {
> public static void main(String[] args) {
> System.out.println("Hello World");
> }
> }
Hello World
Your java program will most likely look a little something like the example above.
public class HelloWorld
is what's called a class header. Class names are usually written with the first letter of every word capitalized; e.g. public class TestClass
.
public static void main(String[] args)
is your main method. The main method is always written in this format.
We'll be going over what each keyword stands for later. For now just know that your program must have a class and a main method to run.
Java can be used to interpret basic mathematical expressions.
> 2 + 2
4
> 10 % 5
0
Assume A = 4 and B = 2
Operator | Name | Example |
---|---|---|
+ | Addition | A + B = 6 |
- | Subtraction | A - B = 4 |
* | Multiplication | A * B = 8 |
/ | Division | A / B = 2 |
% | Modulus | A % B = 0 |
++ | Increment | A++ = 5 |
-- | Decrement | A-- = 3 |
In order to process data, computers need it to be organized by specific type. A word is different from a number and to determine the difference between the two we use data types to specify which one is which so we can process it accordingly. Java contains 8 primitive data types, primitive meaning that they are predefined in the language.
Outside the realm of data types that are predefined are reference data types. An example of such would be the String data type which lets you hold sentences or strings within it; e.g. "Hello World" is a string. There is a separate page introducing strings in the wiki.
Of the 8 primitives, 4 deal with integer numbers and 2 deal with decimal numbers.
Integers are whole numbers that can be positive, negative, or zero. The main difference between the 4 integer types is the range of numbers you can use.
Type | Size | Range |
---|---|---|
long | 64-bit | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-2^63 to 2^63 - 1) |
int | 32-bit | -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1) |
short | 16-bit | -32,768 to 32,767 (-2^15 to 2^15 - 1) |
byte | 8-bit | -128 to 127 (-2^7 to 2^7 - 1) |
int will probably be your most commonly used integer type. Just be aware that others exist as well.
Double and float are the two data types that express decimal numbers like 3.14159. Again, the only difference between them is the range of numbers.
Type | Size | Range |
---|---|---|
double | 64-bit | 6-7 significant decimal digits |
float | 32-bit | 15 significant decimal digits |
The char data type holds characters. This basically means the alphabet: A-Z, a-z, etc. More specifically chars hold unicode characters which contain basically any and all written symbols.
Last but not least, the boolean data type. This can only hold one of two possible values: true or false. The default value of the data type is set to false.
Now that you're aware of the different data types available, you can assign values to variables.
What is a variable? Variables are placeholders for data. It's no different than a variable in algebra where for example, x = 5.
Here's what it looks like in java:
> int x = 5;
> double pi = 3.14;
> char myVariable = 'x';
In order to set a variable, you must first declare it's type. If you want to assign an integer you would use int
followed by the variable name. int myVariableName = 10
is the result. Now myVariableName
holds the value of 10. You can also assign a variable to a variable like in the example below.
You can find this code example under 1 - Basics file in the repository.
> public class CircleArea {
> public static void main(String[] args) {
> double pi = 3.14159;
> double radius = 10;
> // You can also just declare the name of the variable
> // without setting it equal to anything to use later on
> double area;
> // A = pi * r^2
> area = pi * radius * radius;
> // Prints out the resulting value
> System.out.println(area);
> }
> }
314.159
Using the System.out.println()
statement you can not only print out words but also whatever is stored in a variable. Sometimes you may need to combine information and print out a string as well as some data. This is done by concatenation.
To concatenate two separate sets of data you use the +
symbol
For example:
> String hi = "Hello World!";
> String age = "This is my 3rd program!";
> System.out.println(hi + age);
Hello world!This is my 3rd program!
What if you wanted a space between the two sentences? You can either add one in the actual string, or you can add one within the print statement:
> String hi = "Hello World!";
> String age = "This is my 3rd program!";
> System.out.println(hi + " " + age);
Hello world! This is my 3rd program!
In Java, you can also concatenate together different data types. So say you wanted to print out a string as well as a variable you declared earlier, this is how you would do it:
> int x = 5;
> System.out.println("The value of x is: " + x);
The value of x is: 5
For more information on string manipulation visit this wiki page.
We've gone over how to use System.out.println()
to output information on screen. In this section you'll learn how to take in inputs from a user to use in your program.
In order to get input from a user, we must make use of the Scanner
class. Scanner
however is not part of the original package. This means that we must import it:
> import java.util.Scanner;
>
> public class Example {
> public static void main(String[] args) {
> Scanner kb = new Scanner(System.in);
> ...
In this example, we imported the Scanner class and in the main method we declare and initialize the Scanner object.
We can now use the Scanner's many methods to detect various inputs. For example, if you wanted to ask a user their age and receive their input it would look a little something like this:
> import java.util.Scanner;
>
> public class Example {
> public static void main(String[] args) {
> // declare and initialize Scanner
> Scanner kb = new Scanner(System.in);
> // ask user for input
> System.out.print("Please enter your age: ");
> // declare an integer for age and receive input using Scanner's nextInt() method
> int age = kb.nextInt();
> // output age
> System.out.println("You are " + age + " years old");
> // close Scanner
> kb.close();
> }
> }
Please enter your age: 9001
You are 9001 years old
There are many more methods for the Scanner class that allow you to detect inputs for different things. You can use your IDE to explore the different methods that are available to you.
Create a program that asks the user for the size of a square and then calculates and outputs area and perimeter.
Output should look like this:
Size of square:
10
Area: 100
Perimeter: 40