Showing posts with label java concept. Show all posts
Showing posts with label java concept. Show all posts

difference between return; and return (xx); in Java | Java concept

java return keyword is one of the most frequently used keyword in java and also one of the concept, which most programmers say they are well aware of. But, I still meet programmers who say that they are little confused when they come down to return statement. Especially with respect to the method which has void as their return type. So, this post would help you in clearing those doubts and help you understand the clear picture of it... in the most simplest way.
We all know that Java return keyword is used to return the control back to calling method with a certain data-type which was promised during the method declaration. As many wanted to know if we can use just the return keyword(like this: return;) but not return any value(like this: return obj;), then I have to say yes to that. Se the below java programs which will help you understanding the difference better,

public class ReturnTypes {
    public static void main(String args[]){
        functionVoid();
       
functionType();
    }
   
    public static int
functionType(){
        return 1;
    }

    private static void
functionVoid() {
        System.out.println("1");
        if(true){
            return;
        }
        // return;  // This line if UnCommented, would result in Compilation error
        System.out.println("2");
    }
}

If you see in the method functionType() the return keyword is used to return an primitive int value, which is quiet normal and know to almost every java programmer.

But, If you see the method functionVoid() it in fact uses the controversial return keyword(just "return;") which doesn't return anything at-all. Yes this is absolutely valid to use it that way... Ahaaaa hold on, I am not yet done completely... and now imagine if you can use the same return type outside the scope of if statement, as It's already available in program but commented out... That will certainly thorough you an compilation error.

Now why so.. There is nothing wrong with the syntax of return; statement but the error is something different as the compiler is just complaining about "Unreachable Code"... 

By now you know that we can use both of these conventions and are very much valid. But you can ask yourself a question on why actually java needs a return; statement in the first, when we already know that the method is not meant to return any thing back to the calling function as we have declared it void...??

The answer is, you can treat return keyword as a form of goto statement. As it will directly move the control to the line where the method is called. And if you want to accomplish that in an void method then what other option you have to do that other than return; statement.

What is String args[] in Java main method | Why use String args[] in main method | java concept

 What is String args is used for or Why we use String arg[] in main method is a common doubt among Java beginners. First thing newbies exposed while learning Java programming language is writing HelloWorld program in Java, Though HelloWorld is smallest sort of program, it contains lot of useful information and concept for newbies Java programmers, one of them is main method and it's different attribute. Though I covered bit of String args[], while discussing Why main is public static in Java, I thought to write this article to explain String args array in more detail. Let's see a simple Java HelloWorld again to understand more about String args in Java.

public class HelloJava{
      
        public static void main(String args[]){
           System.out.println("Helloworld in Java");
        }
}


What is String args in Java main methodString args[], which is actually an array of java.lang.String type, and it's name is args here. It's not necessary to name it args always, you can name it strArray or whatever you like, but most programmer prefer to name it args, because that's what everyone else do. When we run a Java program from command prompt, we can pass some input to our Java program. Those inputs are stored in this String args array. For example if we modify our program to print content of String args[], as shown below:

public class StringArgsTest {

    public static void main(String args[]) {

        System.out.println("String arguments passed while running this Java Program : ");
        for(String argument : args){
            System.out.println(argument);
        }     
    }   
}



and after compiling, we run this Java program as  java StringArgsTest Java J2EE Android, then this program will print following output :

String arguments passed while running this Java Program :
Java
J2EE
Android

because, we have passed three arguments separated by white space. If you want to combine multiple words, which has some white space in between, then you can enclose them in double quotes. If we run our program again with following command :

java StringArgsTest "This is one argument" "This is another argument"

it will print:

String arguments passed while running this Java Program :
This is one argument
This is another argument

By the way, if you are using Eclipse to run Java Program, then you can provide String argument to your Java program using "Run Configuration". When you run Java program, by right click on Java class with main method, it creates a Run Argument for that class. you can access them as Right click-->Run As-->Run Configuration. In second tab, you can see two section Program Arguments and VM Arguments. You can pass all your String arguments from this program arguments section. As shown in this image

By the way, you can write your String args[] as String[] args as well, because both are valid way to declare String array in Java. Another interesting thing to note is that, from Java 5 onwards, you can varargs or variable arguments in main method instead of String args[]. Which means following declaration of main method is also valid : public static void main(String... args), and you can access this variable argument as normal array in Java. E.g. if we use this syntax of main, in your earlier program we don't need to change anything else, it would work, just like it is working now.