Java tutorial#part3
Hello friends today we will discuss more on data types.
So, let's start,
if we write something like this:
class main{
public static void main(String[] args){
int a=1;
int b=2;
System.out.println(a+b);
}
}
Then the output will be: 3
!!!!Attention not: 12
But if we write something like this then:
class main{
public static void main(String[] args){
int a=1;
int b=2
System.out.println(a+""+b);
}
}
Then the output will be: 12
Also the same with char & Strings.
Here's the code
class main{
public static void main(String[] args){
String i="hello";
String y="world";
System.out.println(i+" "+y);
}
}
OUTPUT: hello world
We can also arithmetical operations:
class main{
public static void main(String[] args){
int a=3, b=2;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
}
}
OUTPUT: 5
1
6
1
BUT Why the a/b is not returning a decimal value:
Because we have to typecast it to float like this:
class main{
public static void main(String[] args){
int a=3, b=2;
System.out.println((float)a/b);
}
}
Then the output will be: 1.5
So see you next time. bye!!
good
ReplyDelete