Data types:

Java Is a Strongly Typed Language.
  because of the following reasons,
First, every variable has a type, every expression has a type, and every type is strictly defined.
Second, all assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility.
There are no automatic coercions or conversions of conflicting types as in some languages.
The compiler checks all expressions and parameters to ensure that the types are compatible.
Q) Any, type related errors will be detected by   whom?

AnsCompiler














The Primitive Types (Simple types)
Java defines eight primitive types of data:
  byte, short, int, long
   float, double
  char &
  boolean
Integers This group includes byte, short, int, and long, which are for whole-valued signed numbers.
Floating-point numbers This group includes float and double, which represent numbers with fractional precision.
Characters This group includes char, which represents symbols in a character set, like letters and numbers.
Boolean This group includes boolean, which is a special type for representing true/false values.

Q) Are the follwing statements valid?
  int a=10.5;//INVALID
  float f=39.2; //INVALID
  char ch=65;//VALID
  boolean b=0; //INVALID
  char ch2=“a”; //INVALID
  int d=09; //INVALID
Try to provide the reason for each statement.
.................................................................................................................................................................................................

Integers

Java defines four integer types: byte, short, int, and long.
All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers.
Q) Among all integers, which data type will be used mostly?
Ans: int
Q) Tell me, some uses of int type, where we use it compulsorily?
Ans: array size,loop control variables....etc
Q) What is the output of the following program?
class MyClass{
  public static void main(String args[]){
  byte b=20;
  byte c=30;
  byte d=b+c;
  System.out.println(d);
  }
}
Output: Compile time error
Reason:  when byte and short values are used in an expression they are promoted to int when the expression is evaluated.

long
is useful for those occasions where an int type is not large enough to hold the desired value.
Q) Here, I have a value i.e 45. What type of value it is?
Ans: by default any whole numbered value is considered as int type.

Floating-Point Types
Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision.
Java implements the standard (IEEE–754) set of floating-point types and operators.
There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively.

Q) Which one is accurate float or double?
Ans: double (because it is double precision.So, after decimal point it gives up to 15-16 digits correct/exact data. Whereas in single precision up to 7-8 digits only give correct/exact data )
Q) Here, I have a value i.e 39.33… What type of value it is?
Ans: double(by default any fractional value is double type value in java)
Q) What is the output of the following program?
class MyClass2{
  public static void main(String args[]){
  float f=30.55;
  System.out.println(f);
  }
}
Output:Compile time error
Reason: double is larger than float. So, we can not accommodate double value in float variable.

Try to modify the above code for not getting error.
Generally you may do the following:
                             1. You may try to change the type of variable f from float to double.
                             2. You may try to do type casting.
                             3. You may append f or F to the value 30.55 as 30.55F.
Yes, all will give you a valid solution.But ensure that whether you need accurate value or not?


Characters
In Java, the data type used to store characters is char.
However, C/C++ programmers beware:
                --char in Java is not the same as char in C or C++.
Q) How many bits are required for char type in C/C++?
Ans: 8 bits
Q) How many bits are required for char type in java?
Ans: 16 bits
üJava uses Unicode to represent characters.
Unicode defines a fully international character set that can represent all of the characters found in all human languages
--This is why they reserved one more byte extra in java (to support all human language characters).
The range of a char is 0 to 65,536.
Note: There are no negative chars.
Q) What is the output of the following?
class MyClass3{
  public static void main(String  args[]){
  char ch=65;
  System.out.println(ch);
  }
}
Output:  A
Reason: Here, we assigned UNICODE value to the variable ch.(Characters are subset of integers)

Booleans
Java has a primitive type, called boolean, for logical values.
It can have only one of two possible values, true or false.
              --Size/width of boolean  type is specific to JVM.
Q) What is the outcome value type of a relational expression?
Ans: boolean type
Q) Is the following statement correct?
  boolean b=0;
  No (In java false is != 0 and true is !=1 like in C language)--> Java is strongly typed language.
Q) What is the output of the following?
class MyClass4{
  public static void main(String  args[]){
  boolean b=10>9;
  System.out.println(b);
  }
}
Output:  true



Comments

Popular posts from this blog

Type Conversion and Casting