Literals : Primitive Data Type « Data Type « Java






Literals

     
//: c03:Literals.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

public class Literals {
  
  char c = 0xffff; // max char hex value
  
  byte b = 0x7f; // max byte hex value
  
  short s = 0x7fff; // max short hex value
  
  int i1 = 0x2f; // Hexadecimal (lowercase)
  
  int i2 = 0X2F; // Hexadecimal (uppercase)
  
  int i3 = 0177; // Octal (leading zero)
  
  // Hex and Oct also work with long.
  
  long n1 = 200L; // long suffix

  long n2 = 200l; // long suffix (but can be confusing)

  long n3 = 200;

  //! long l6(200); // not allowed

  float f1 = 1;

  float f2 = 1F; // float suffix

  float f3 = 1f; // float suffix

  float f4 = 1e-45f; // 10 to the power

  float f5 = 1e+9f; // float suffix

  double d1 = 1d; // double suffix

  double d2 = 1D; // double suffix

  double d3 = 47e47d; // 10 to the power
} ///:~



           
         
    
    
    
    
  








Related examples in the same category

1.Use Integer constructor to convert int primitive type to Integer object.
2.Convert Java Integer object to Numeric primitive types
3.Convert Java String to Integer object
4.Create an Integer object
5.Arithmetic DemoArithmetic Demo
6.Max Variable Length DemoMax Variable Length Demo
7.Data Type Print TestData Type Print Test
8.Tests all the operators on all the primitive data types
9.Demonstrates the ++ and -- operatorsDemonstrates the ++ and -- operators
10.Demonstrates the mathematical operators.Demonstrates the mathematical operators.
11.Java lets you overflowJava lets you overflow
12.Built in typesBuilt in types
13.Shows default initial valuesShows default initial values
14.Relational DemoRelational Demo
15.Parse Number
16.Java Type Helper
17.Convert the given array (which may be a primitive array) to an object array
18.Convert primitive back and forth
19.Returns a default value if the object passed is null
20.A mutable boolean wrapper.
21.A mutable byte wrapper.
22.A mutable double wrapper.
23.A mutable float wrapper.
24.A mutable int wrapper.
25.A mutable long wrapper.
26.A mutable short wrapper.
27.Primitive utilities