Java Basics: Data Types Data Types: (4 Types)
Java Basics: Data Types Data Types: (4 Types)
Data types
Java Basics 1
int myFirstNumber = 5; // -> expression
Int
Short 16 16
Int 32 32
Casting in Java
data type → variable name = (data type/ CAST) value
Java Basics 2
The value of [myTotal] = 2 which is of the type int
int myTotal = 20 / 10
To solve this issue we must specify the type of the value as well
using Casting
So the retuned type of the value must be of the same type as the
variable
Java Basics 3
Float & Double
Double 64 64
The int will return 1 because int only return a whole number
// Regular Characters
char myChar = 'D';
char myChar = 'DD'; // will retun an error since DD is more than one character
Java Basics 4
Strings
Strings are immutable
if(isAlien == false)
System.out.println("It is not alien!");
Java Basics 5
int newValue = 50;
if(newValue == 50) {
System.out.println("This is an error");
}
Ternary Operator
boolean wasCar = isCar ? true : false;
If isCar = true then wasCar = true, else wasCar = false
The code block inside the if statement gets executed only if the parameter
gameOver is true
Java Basics 6
public class Main {
if(gameOver){
int finalScore = score + (levelCompleted * bonus);
finalScore += 1000;
System.out.println("Your final score was " + finalScore);
}
}
}
Now if we want to add a second score, levelCompleted and bonus and print out
the finalScore we can use the existing variables, assign them new values
and then duplicate the if statement.
Remember that when we will assign new values to already defined variables we
will not specify their data types again ( no boolean & int ) since we do not want
to declare the variables again, but to assign new values to them.
Whitch is called code duplication and it IS NOT a good practice.
Imagine if we want to add 50 new game results. We will end with 1000 lines of
duplicated code. That's a nono.
if(gameOver){
int finalScore = score + (levelCompleted * bonus);
finalScore += 1000;
System.out.println("Your final score was " + finalScore);
}
score = 10000;
levelCompleted = 8;
bonus = 200;
if(gameOver){
int finalScore = score + (levelCompleted * bonus);
System.out.println("Your final score was " + finalScore);
Java Basics 7
}
}
}
Duplicate code is bad, so let's see how we can create an method that
we can reuse in order to add as many new values we want without
duplicating existing code.
Step #1
We will work inside the public class Main {}
Step #2
We will define the values inside the main method by calling a new method inside
here that will perform the calculation
Step #3
Now we will create a new method that will calculate our finalPrice
public static void calculateScore(// we will need to pass some parameters here) {
}
Step #4
Java Basics 8
Now let's pass some parameters inside the () so we can use those parameters to
create the if statement and calculate the finalPrice
This way we can always change the way the finalScore is calculated (adding
1000 extra points, etc) and we can do it in only one place. We will not need to
make the change inside all the places where the code was duplicated.
public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {
}
Step #5
Now that we have the needed parameters let's use them inside the if statement
and calculate the finalPrice
public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {
if (gameOver) {
int finalScore = score + (levelCompleted * bonus);
finalScore += 2000;
System.out.println("Your final score was " + finalScore);
}
}
Java Basics 9
For the second way we will define the variables and then use the variable names
as arguments
Method Overloading
Guide to Overloading Methods in Java
Java defines a method as a unit of the tasks that a class can perform.
And proper programming practice encourages us to ensure a method
does one thing and one thing only. It is also normal to have one
https://stackabuse.com/guide-to-overloading-methods-in-java
Java Basics 10
But in order to do that every method needs to have it's
unique signature.
Java Basics 11
As we can see above, all methods have the same name,
but they have have a different number and type of
parameters.
int switchValue = 3;
switch (switchValue) {
case 1:
System.out.println("Value was 1");
break;
case 2:
System.out.println("Value was 2");
break;
default:
System.out.println("Was not 1 or 2");
break;
}
While
Java Basics 12
Here we will create a loop that outputs all the even numbers in a
range
If the number is not even(ex. 5) , continue will skip the printing and go back to
(number <= finishNumber)
If (number <= finishNumber) is evaluated as true, it's gonna increase the number(ex
ro 6) and check if it's even, if it is, then it' gonna print it and do the same thing untill
(number <= finishNumber) will be evaluated as false
int number = 4;
int finishNumber = 20;
Java Basics 13
Java Basics 14