Jump to content

Tiemen

Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by Tiemen

  1.  

    This code below clarifies some part of my confusion in the first part of my question.

    But the second and 3th question remains. 

    public class MyClass {
      public static void main(String[] args) {
    
        // Code here CANNOT use x
    
        { // This is a block
    
          // Code here CANNOT use x
    
          int x = 100;
    
          // Code here CAN use x
          System.out.println(x);
    
       } // The block ends here
    
      // Code here CANNOT use x
    
      }
    }
  2. So in the example below they want to give a example that multiple methods can have the same name with different parameters:

     

    I have a few questions:

    I thought variables had unique names, how does it knows we talk about the same x + y. I assume the variables are only defined within the method then, right? And not within the whole public class MyClass

     

    Second: 

    Why does the double return the value 10.5599999999999 instead of 10.56

    If I edit 6.26 to 6.27 it will return 10.57

     

     

    public class MyClass {
      static int plusMethodInt(int x, int y) {
        return x + y;
      }
      
      static double plusMethodDouble(double x, double y) {
        return x + y;
      }
      
      public static void main(String[] args) {
        int myNum1 = plusMethodInt(8, 5);
        double myNum2 = plusMethodDouble(4.3, 6.26);
        System.out.println("int: " + myNum1);
        System.out.println("double: " + myNum2);
      }
    }
    
    Result:
    int: 13
    double: 10.559999999999999
     
    Third
    Why call the method the same if your using different types of variables. Will  those variables called x be usable if you call on x and there is a value int 2 and float 4.21
    Example in tutorial.
    int myMethod(int x)
    float myMethod(float x)
    double myMethod(double x, double y)
    
    Thank you
     
×
×
  • Create New...