Back to feed
Renewal·서른의 생활코딩

Day 3) Getting to Know Variables 2 (Boolean, char, Integer, Float, String, Casting) _20180530 AM

NS
normalstory
cover image

package a_variable;

/**

 * About variable types

 * @author BCS

 *

 */

public class VariableOther {

public static void main(String[] args){

/*

5. Boolean type — boolean (default false)

A boolean variable can store one valuetrue or false

A boolean variable represents an answer (yes, no) or a switch (on/off).

    Boolean itself uses only 1 bit,

but since the minimum unit for handling data is 1 byte, it takes a 1-byte form.

Problem

Declare and initialize a variable named abc that can hold the value false.

*/

boolean abc= false;

System.out.println(abc);

/*

6. Character type — char (' \u0000 ')

- Uses a 2-byte (hexadecimal) system.

- JAVA uses the Unicode character system. 

- ' A ' = 65 (decimal = DEC = int type) = 41 (hexadecimal = HEX)

*/

char a = 'A' ;  //= 65(10 DEC)

char b = '\u0041' ;   //= 65(10 DEC)

char c = 65 ;   //= 65(10 DEC)

System.out.println(a);

System.out.println(b);

System.out.println(c);

/*

7. Integer typesbyte, short, int, long

- The default is int.

- To store a value, you need to choose one of the four integer types based on the range.

- byte and short are small, so going out of range can give wrong results.

So int is preferable.

- The JVM operand stack stores operands in 4-byte units, 

      so types smaller than 4 bytes are converted to 4 bytes before operations.

  - When declaring long, if the number is large, append 'L' at the end.

   - When declaring float, if the number is large, append 'f' or 'F' at the end.

  - overflow: in integer types,

  when a variable goes beyond its storable range, it wraps around from the minimum value.

  You can't initialize a variable with a value outside its range.

Problem

  Declare and initialize a variable named 'b2' that can store the value 50.

*/

int b2 = 50;  // int is commonly used. 

// short b2 = 50;  // also possible

System.out.println(b2);


// long long2 = 982323423432324423; 

long long2 = 982323423432324423L;  

// declared as long; for large numbers, append 'L' at the end


// byte b3 = 128;  // out of range

byte b3 = 126;

System.out.println(b3);   //126

b3++; // increment by 1

System.out.println(b3);   //127


b3++; // increment by 1

System.out.println(b3);   //-128 (wraps to the smallest value on overflow)

// = overflow (only in integer types)

b3++; // increment by 1

System.out.println(b3);   //-127 (wraps to the smallest value on overflow)

/*

8. Floating-point types: float, double — store real numbers

     bit layout

float: 1+8+23

double: 1+11+52

    When picking a floating-point type, precision matters as much as range.

            (how many decimal places you need matters)

The default is double. because it can be expressed as "2 to the n-th power"

*/

double d2 = 1.2345689876543213456786543; // long number, but since it's the default, no 'L' suffix

System.out.println(d2); //-> 16 digits

// float d2 = 1.2345689876543213456786543;  // error — float isn't the default, so add 'f' or 'F'

float f2 = 1.2345689876543213456786543f; 

System.out.println(f2); //-> 7 digits

double d3 = 0.1;

float f3 = 0.1f;

System.out.println(d3 == f3); 

double d4 = 0.5;

float f4 = 0.5f;

System.out.println(d4 == f4); // when divisible by powers of 2, double and float have the same value.


/*

 9. String  - a Class that handles 'strings'

only ' \ ' can't be used

' \t ' is used = spacing like tab

' \n ' is used = a new line break

*/

//char cc = '  ' ;   // error — empty single quotes is an error

// primitive types store a value; a range exists.

String str1 =null ; // default value is null (before the apartment is built)

String str2 =" " ;      // fine

// reference types store an address. (address = the apartment is built)

// no range exists

System.out.println(5+"sdf");  // different types. 

// integer, string -> converts to the "stronger" type

// "5" + "sdf"

System.out.println("6"+"7");

System.out.println(true+"1234");

System.out.println("=======-----------");

System.out.println("=======\t-----------");

System.out.println("=======\n-----------");

/*

10. Casting

Converting the type of a variable or literal to another type.

Only possible among primitives.

*/

int i3 =65;

byte b6 = (byte)i3; // strong to weak, overflow guard, use the cast operator ()

System.out.println(b6);


char c6 = (char)i3; // strong to weak, overflow guard, use the cast operator ()

System.out.println(c6);

// float f6 = 3.141592; // error

float f6 = 3.141592f;

int i4 = (int)f6; // strong to weak, overflow guard, use the cast operator ()

System.out.println(i4);

int i5 = b6; // weak to strong, cast operator () can be omitted 

}

}



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min