0
Variables, intregers
So I just started learning java and the lesson on variables and the lesson on integers were both a little fast paced and I don’t feel like I learned the information that well. Could someone explain those 2 better and give an example of some modulos?
2 Respostas
+ 5
This is a statement for assigning an integer value to variable b.
int b = 26;
A shortcut of 2 statements:
int b;
b = 26;
Where:
int b; says that b is a variable, that can store an integer value.
b = 26; says that b stores an integer value of 26.
int --> datatype heading.
b --> variable name.
26 --> value.
The datatype heading defines, what kind of values can be stored to its variable.
You can also create variables with other datatype headings, 4 basic headings:
int
float
double
String
int is a short for integer,
integers are whole numbers.
int wholeNumber = 83;
float is a short for floating point number,
floating point numbers are decimal numbers.
float decimalNumber = 7.2
double is another version of float.
double decimalNumber2 = 6.0
String is a sequence of characters.
Strings can be thought as text objects.
Any text in any software is often handled as strings.
String text = "I have fiftyfive apples.";
+ 2
A variable stores data of its type.
int x = 10;
⬆️Here the value of 10 is stored inside x which means x is now 10.
int x = 10%2;
⬆️Here as I know that 10 mod 2 equals 0 and that 0 is a int value, the result can safely be stored inside the datatype int.
Strings are abit diffrent they are objects which simply means they may have methods that return things you can accsess these by using the dot . operator, you cant do this with primitive types like int.
For example I will create a string and then find its length which returns a int value.
String str = "hello";
As strings are objects they work slightly diffrent then primitive types like "int" as primitive types store the actual value, object types dont they infact store a refrenece as there value this means you can use dot operator as a handle to use its methods.
int z = str.length();
As the length of a string returned is a number then you need to store it in a variable which stores numbers that would be the datatype int.
Hope that helps.