Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Advanced Programming Languages This is a discussion topic please go into detail

ID: 3663799 • Letter: A

Question

Advanced Programming Languages This is a discussion topic please go into detail discussing a case where declaring two local variables with the same name in same method is permitted and one where it is prohibited? ***Question and instructions: Let's consider Java, a language with which everyone should be familiar, and explore its scope rules. One aspect of the scope rules of any language is when data or methods can have the same name. Give examples in Java to illustrate a case where declaring two local variables with the same name in same method is permitted and one where it is prohibited? **** please don't just copy something off the internet and answer the question. This has to be in your own words and not a novel copied from a website or document. (The teacher also has access to the internet sites your copying from) Thank you for your help

Explanation / Answer


class main{
   // allowed
   public static int func(int c){
       return c+2;
   }
   public static void func(String c){
       System.out.println(c);
   }
   public static void main(String[] args){
       int r = func(3);
       func("str");

       // Not allowed
       int x = 5;
       double x = 5.0;
       System.out.println(x+" AND "+x);
   }
}

in Java same name to variable are not allowed as java compiler can not understand which x you are actaully referring to.
so this type of code may be correct syntactically but they can not be compiled.

yes we can two methods with similiar name in same scope but either return type must be different or their number of argument or argument type must be different

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote