Variables are important pieces in programming. We are using them to store data values. In java, we have different types of variables:
int
float
char
boolean
byte
short
long
double
String
(I don’t know why it’s capital 😥)First specify the type and give it a name and then assign it a value.
variable_type variable_name = value;
For example:
String userName = "Berkay";
int likeCount = 5;
Also, you can declare a variable without value.
char c;
c = 'b';
If you assign it new value, it will update itself.
String country = "Turkey";
System.out.println(country); // Turkey
country = "England";
System.out.println(country); // England
Let’s learn the types with examples.
int number1 = 10;
double number2 = 4.5;
// you have to cast float
float number3 = (float) 4.5;
number3 = 4.5f; // this is shorter way
char character = 'c';
String string = "string";
Boolean type only accepts true
or false
.
boolean bool = false;
bool = true;
You can merge strings with +
. Let’s see it in action.
public class MyClass {
public static void main(String args[]) {
String fileName = "index";
String fileExt = "html";
System.out.println("Your file => " + fileName + "." + fileExt);
}
}
Output:
> Your file => index.html
I'm writing a book to simplify Hugo and allow you to create your own theme just from scratch.
Join the waitlistBook a call with me and maybe we can combine our forces to make your project a reality.