Java Variables

Thu Nov 19 2020

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 😥)

How to create or declare variables

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.

Numbers

int number1 = 10;
double number2 = 4.5;
// you have to cast float
float number3 = (float) 4.5;
number3 = 4.5f; // this is shorter way

Characters and Strings

char character = 'c';
String string = "string";

Boolean

Boolean type only accepts true or false.

boolean bool = false;
bool = true;

Extra 😉

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

Simplified Hugo Book

I'm writing a book to simplify Hugo and allow you to create your own theme just from scratch.

Join the waitlist

Let's make your dream project real!

Book a call with me and maybe we can combine our forces to make your project a reality.

Book a call now!