Variables and Strings in Java
Variable is
a container used to store a value and it behaves like the value stored in it.
To store a
variable, you will need an allocation in memory where the variable will be
stored and this can be done using data types that will store the variable.
Below are the Different Data types and their values
DATA TYPE SIZE REFERENCE VALUE
byte 1 bit primitive
-128 to 127
short 2 bytes primitive -32K to32K
int 4
bytes primitive
-2B to 2B
long 8 bytes primitive
boolean 1 bit primitive true
or false
float 4
bytes primitive fractional numbers up to
6 digits
double 8 bytes primitive fractional numbers up
to 15 digits
char 2
bytes primitive single
characters eg. A, B,
String varies Reference sequence of
Characters
The highlighted variables are the most used ones and are
those we are going to tackle in this lesson.
Example
String name = "Nate";
Nate is stored in the variable name. Whenever you need to
refer to Nate, You don’t have to write is but rather just by using name gives
you the value stored in it.
More Examples
//datatype variable name value
String name = "Nate"; // This prints out a String
int age = 18; // This prints an integer value
long salary = 39_201_292_131L; // This prints out integer value that a normal int can't store
double height = 12.232; // Double stores decimals
float weight = 75.34F;
boolean isMale = true;
char sign = '@'; // Stores single characters
// How to print out the values of the variables stored above
System.out.println(name);
System.out.println(age);
System.out.println(salary);
System.out.println(height);
System.out.println(weight);
System.out.println(isMale);
System.out.println(sign);
Variables stored can be later changed without changing the
original value
Example
String name = "Nate";
name = "Mike";
System.out.println(name);
Output
Mike
Declaration and Initialization of Variables in Java.
A variable can be declared without giving it any value. This
process is called declaration. A value can be later assigned to it in the
course of writing your program.
Example:
String name;
name = "Mike";
On the other hand, when a value is passed immediately after
declaring a variable, it is called initialization. The value can be later
changed in the course of writing your program.
Example
String name = "Mike";
“Move on to the next lesson with more on Strings and how it
differs from other datatypes.
An exercise will be given and your solution will be marked
after solving it to make you more conversant in what you are learning.”
Strings
Strings are reference datatypes because they have abstract method stored in them that can be operated on the variable the store.
Example
String name = "John";String lastName = "Mike";
System.out.println(name.toUpperCase());System.out.println(name.toLowerCase());System.out.println(name.charAt(3));System.out.println(name.concat(lastName));System.out.println("Hello " +name);System.out.println("You are " + age + " years old");System.out.println("You weigh " +weight+ " and you are " + height+ " tall");System.out.println("you are" + weight + height);// Concatenation
0 Comments