Del-Norte-Farmers-Iowa-Hawkeyes-Fanclub / IHF-Frontend

Blogs and Issues for project Planning
https://del-norte-farmers-iowa-hawkeyes-fanclub.github.io/Del-Norte-Association-of-Sustainable-Farmers-Saftey-OSHA-and-Fantasy-Log-Book/
Apache License 2.0
0 stars 0 forks source link

Value Types Reference Issue - Derrick #5

Open Pitsco opened 8 months ago

Pitsco commented 8 months ago

Value Types (Primitive Types)

General Information

Differences

  1. Number and Definition:

    • Reference Types: Unlimited number, defined by the user.
    • Primitive Types: Limited and predefined in the language.
  2. Memory Location:

    • Reference Types: Stores a reference to the data in memory.
    • Primitive Types: Stores the actual data held by the variable.
  3. Assignment (Note the Difference!):

    • Reference Types: When assigned to another reference type, both references point to the same object in memory.
    • Primitive Types: When assigned to another variable of the same type, a copy of the data is made.
  4. Method Parameter Passing:

    • Reference Types: When an object is passed into a method, the method can change the contents of the object, but not the address/reference of the object.
    • Primitive Types: When a primitive is passed into a method, only a copy of the primitive is passed. The called method does not have access to the original primitive value and can only change the copied value.
public class PrimitiveTypesExample {
    public static void main(String[] args) {
        // Declaration and initialization of primitive variables
        boolean isJavaFun = true;
        char grade = 'A';
        byte byteValue = 127; // byte range: -128 to 127
        short shortValue = 32000; // short range: -32,768 to 32,767
        int intValue = 42;
        long longValue = 123456789L; // The 'L' indicates a long literal
        float floatValue = 3.14f; // The 'f' indicates a float literal
        double doubleValue = 2.71828;

        // Displaying values
        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Grade: " + grade);
        System.out.println("Byte Value: " + byteValue);
        System.out.println("Short Value: " + shortValue);
        System.out.println("Int Value: " + intValue);
        System.out.println("Long Value: " + longValue);
        System.out.println("Float Value: " + floatValue);
        System.out.println("Double Value: " + doubleValue);
    }
}