Posts

Showing posts from July, 2017

Type Conversion and Casting

Q)   Which one is valid in the following?        int a=3.14;// INVALID      double d=35;//VALID Tell me the reason, Ans : As per the behavior of java data types I am listing out the order(numeric/integral) : (smaller to larger)                           byte                           short                           int                           long                          float                         double (So, we can not assign larger type value to a smaller type variable.) In java, we have two kinds of conversions:   1....

Literal representation:

Literals (Constant values) (refer data types concept before proceeding this..) Integer Literals   Integer literals can be denoted in the following ways:   1. Decimal representation(base 10)   2. Octal representation(base 8)   3. HexaDecimal representation(base 16)   4. Binary representation(base 2)--->added in  java 7 Q) What is the output of the following code? class MyClass5{   public static void main(String  args []){   int b=09;   System.out.println (b);   } } Output: Compile time error Reason: Given number is in octal form, whose digits range is in between 0 to 7. Examples: 1,10,23,45,345,43232,…. à All these are in decimal form. 01,03,012,0345,02436,…. à All these are in Octal form. 0x1,0x22,0x324,0xA213,0x123F,…. à All these are in hexadecimal form 0b0000,0b1101,0b10101,0b11110,…. à All these are in binary form Floating-Point Literals – Floating-point number...

Data types:

Image
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? Ans :  Compiler 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...