Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;

/** ****************************************************************************
 * PRIMITIVES EXAMPLE CLASS
 * @author stanislav.zorjan
 */
public class PrimitivesExample {
    
    private byte byte0 = 127;
    private byte byte1 = -128;
    private byte byte2 = Byte.MAX_VALUE;
    private byte byte3 = Byte.MIN_VALUE;
    private byte byte4 = Byte.parseByte("11");
    // wrapped byte
    private Byte byte5 = new Byte("3");
    
    private short short0 = 32767;
    private short short1 = -32768;
    private short short2 = Short.MAX_VALUE;
    private short short3 = Short.MIN_VALUE;
    private short short4 = Short.parseShort("123");
    // wrapped short
    private Short short5 = new Short("123");
    
    private int int0 = 2147483647;
    private int int1 = -2147483648;
    private int int2 = Integer.MAX_VALUE;
    private int int3 = Integer.MIN_VALUE;
    private int int4 = 0xff0000;
    private int int5 = '\u0033';
    // wrapped int
    private Integer int6 = new Integer(123);
    private Integer int7 = new Integer("123");
    
    private long long0 = 9223372036854775807L;
    private long long1 = -9223372036854775808L;
    private long long2 = 323_4324_4234L;
    private long long3 = Long.MAX_VALUE;
    private long long4 = Long.MIN_VALUE;
    // wrapped long
    private Long long5 = new Long(10L);
    private Long long6 = new Long("10");
    
    private float float0 = 123435;
    private float float1 = 1.2343F;
    private float float2 = 11223.43242344324F;
    private float float3 = 4324234_453243_4324F;
    private float float4 = Float.MAX_VALUE;
    private float float5 = Float.MIN_VALUE;
    // wrapped float
    private Float float6 = new Float("10");
    private Float float7 = new Float(10f);
    
    
    private double double0 = 123213123;
    private double double1 = 432.4324;
    private double double2 = 234.234234D;
    private double double3 = 234_234_234D;
    private double double4 = Double.MAX_VALUE;
    private double double5 = Double.MIN_VALUE;
    // wrapped double
    private Double double6 = new Double("6.6");
    private Double double7 = new Double(8.2);
    
    private boolean boolean0 = true;
    private boolean boolean1 = false;
    private boolean boolean2 = Boolean.TRUE;
    private boolean boolean3 = Boolean.FALSE;
    private boolean boolean4 = Boolean.parseBoolean("True");
    private boolean boolean5 = Boolean.parseBoolean("false");
    // wrapped boolean
    private Boolean boolean6 = new Boolean(false);
    private Boolean boolean7 = new Boolean("true");
    
    private char char0 = 'c';
    private char char1 = '\u00D1';
    // wrapped char
    private Character char2 = new Character('c');
    
    
    /** ************************************************************************
     * Default constructor
     */
    public PrimitivesExample() {
	printPrimitives();
	//convertPrimitives();
    }
    
    private void printPrimitives(){
	System.out.println("byte");
	System.out.println(this.byte0);
	System.out.println(this.byte1);
	System.out.println(this.byte2);
	System.out.println(this.byte3);
	System.out.println(this.byte4);
	System.out.println(this.byte5);
	
	System.out.println("");
	System.out.println("short");
	System.out.println(this.short0);
	System.out.println(this.short1);
	System.out.println(this.short2);
	System.out.println(this.short3);
	System.out.println(this.short4);
	System.out.println(this.short5);
	
	System.out.println("");
	System.out.println("int");
	System.out.println(this.int0);
	System.out.println(this.int1);
	System.out.println(this.int2);
	System.out.println(this.int3);
	System.out.println(this.int4);
	System.out.println(this.int5);
	System.out.println(this.int6);
	System.out.println(this.int7);
	
	System.out.println("");
	System.out.println("long");
	System.out.println(this.long0);
	System.out.println(this.long1);
	System.out.println(this.long2);
	System.out.println(this.long3);
	System.out.println(this.long4);
	System.out.println(this.long5);
	System.out.println(this.long6);
	
	System.out.println("");
	System.out.println("float");
	System.out.println(this.float0);
	System.out.println(this.float1);
	System.out.println(this.float2);
	System.out.println(this.float3);
	System.out.println(this.float4);
	System.out.println(this.float5);
	System.out.println(this.float6);
	System.out.println(this.float7);
	
	System.out.println("");
	System.out.println("double");
	System.out.println(this.double0);
	System.out.println(this.double1);
	System.out.println(this.double2);
	System.out.println(this.double3);
	System.out.println(this.double4);
	System.out.println(this.double5);
	System.out.println(this.double6);
	System.out.println(this.double7);
	
	System.out.println("");
	System.out.println("boolean");
	System.out.println(this.boolean0);
	System.out.println(this.boolean1);
	System.out.println(this.boolean2);
	System.out.println(this.boolean3);
	System.out.println(this.boolean4);
	System.out.println(this.boolean5);
	System.out.println(this.boolean6);
	System.out.println(this.boolean7);
	
	System.out.println("");
	System.out.println("char");
	System.out.println(this.char0);
	System.out.println(this.char1);
	System.out.println(this.char2);
    }
    
    /** ************************************************************************
     * Primitives conversion
     */
    private void convertPrimitives(){
	// Converting to string
	System.out.println("Byte to String: " + Byte.toString(this.byte0));
	System.out.println("Short to String: " + Short.toString(this.short0));
	System.out.println("Integer to String: " + Integer.toString(this.int0));
	// Coonverting to different "types" of string
	System.out.println("Integer to hex String: " + Integer.toHexString(this.int0));
	
	System.out.println("Integer to binary String: " + Integer.toBinaryString(this.int0));
	System.out.println("Integer to octal String: " + Integer.toOctalString(this.int0));
	System.out.println("Integer to String: " + Integer.toString(this.int0, 10));
	System.out.println("Integer to hex String: " + Integer.toString(this.int0, 16));
	System.out.println("Integer to octal String: " + Integer.toString(this.int0, 8));
	System.out.println("Integer to binary String: " + Integer.toString(this.int0, 2));
	System.out.println("Long to String: " + Long.toString(this.long0));
	
	System.out.println("Float to String: " + Float.toString(this.float0));
	System.out.println("Double to String: " + Double.toString(this.double0));
	System.out.println("");
	
	/* Conversation of all these types is done in the same way, just using 
	 * appropriate "byteValue()", "shortValue()"....
	 *  !!! NOTE !!!
	 * When converting from bigger sizes to smaller like short to byte, be 
	 * aware of loosing precision (float or double) or complete number. 
	 * See how 130 short value is represented as -126 as byte.
	 */ 
	System.out.println("Byte to Byte: "+ ((Byte) this.byte0).byteValue());
	System.out.println("Short to Byte: "+ ((Short) Short.parseShort("127")).byteValue());
	System.out.println("Short to Byte: "+ ((Short) Short.parseShort("130")).byteValue());
	System.out.println("Int to Byte: "+ ((Integer) Integer.parseInt("-11")).byteValue());
	System.out.println("Long to Byte: "+ ((Long) this.long1).byteValue());
	System.out.println("Long to Byte: "+ ((Long) Long.parseLong("-160")).byteValue());
	System.out.println("Float to Byte: "+ ((Float) Float.parseFloat("-200")).byteValue());
	System.out.println("Double to Byte: "+ ((Double) this.double2).byteValue());
	System.out.println("Boolean to Byte: "+ (this.boolean0 ? 1 : 0));
	System.out.println("Boolean to Byte: "+ (this.boolean1 ? 1 : 0));
	System.out.println("Character to Byte: "+ (byte) this.char0);
	System.out.println("Character to Byte: "+ (byte) this.char1);
	
    }
    
}