* void - This is a return type - or, more correctly, specifies that a method has no return type.* protected - This is an access modifier. It says that the only classes that can call this method are subclasses and classes in the same package.* int - This is a value type (or return type).* main - This is the name of the method.
you can understand what is the purpose of return in java after this simple example Basically return use in java to return some values from a method note: this sample will work on all java versions, i test it on JDK 1.4 enjoy the code. file name : Class1.java ----- package mypackage1; //package name public class Class1 //class name + file name the { public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int { int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method return " I'm the returned value "+ answer; ////this is what you need to take from the method } public static void main(String [] arg) { Class1 c1 = new Class1(); //creating c1 object from Class1 Class System.out.println(c1.add(10,6)); //printing the returned values from the method; } } ----------------- output: I'am in Class1 I'am the returned value 16
The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value. By default, it will return zero. To learn more about data science please visit- Learnbay.co
#include <iostream> #include <conio.h> int linearSearch( const int array[], int length, int value); int main() { const int arraySize = 100; int a[arraySize]; int element; for( int i = 0; i < arraySize; i++) { a[i] = 2 * i; } element = linearSearch( a, arraySize, 10); if( element != -1 ) cout << "Found value in element " << element << endl; else cout << "Value not found." << endl; getch(); return 0; } int linearSearch( const int array[], int length, int value) { if(length==0) return -1; else if (array[length-1]==value) return length-1; else return urch( array, length-1, value); } it is in c++ language ,u can change it to c by including stdio.h ,printf & scanf.....i think it wud be beneficial have a look their too. similar program like that and easy understanding http://fahad-cprogramming.blogspot.com/2014/02/linear-search-program-in-c-using.html
The void keyword is used to show that a method will not return a value. // no return type public void setX(int x) { this.x = x; } // returns an int public int getX() { return x; }
* void - This is a return type - or, more correctly, specifies that a method has no return type.* protected - This is an access modifier. It says that the only classes that can call this method are subclasses and classes in the same package.* int - This is a value type (or return type).* main - This is the name of the method.
When you send an int variable to a method, you do so in the method call, for example if you are calling the method Multiply with an int value, Multiply better be set up to accept an int value: public static double Multiply(int a) { //do something //return a double } In the above code, even though you return a double, you accept an int to be passed in from the method call, something like: Multiply(83); but calling Multiply(83.0); would give you an error. The data input types for a method and the return type are totally separate.
you can understand what is the purpose of return in java after this simple example Basically return use in java to return some values from a method note: this sample will work on all java versions, i test it on JDK 1.4 enjoy the code. file name : Class1.java ----- package mypackage1; //package name public class Class1 //class name + file name the { public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int { int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method return " I'm the returned value "+ answer; ////this is what you need to take from the method } public static void main(String [] arg) { Class1 c1 = new Class1(); //creating c1 object from Class1 Class System.out.println(c1.add(10,6)); //printing the returned values from the method; } } ----------------- output: I'am in Class1 I'am the returned value 16
The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value. By default, it will return zero. To learn more about data science please visit- Learnbay.co
public int getValue(String value){ int i; try { i = Integer.parseInt(value) } catch (Exception e){ i = 0; } return i; }
#include <iostream> #include <conio.h> int linearSearch( const int array[], int length, int value); int main() { const int arraySize = 100; int a[arraySize]; int element; for( int i = 0; i < arraySize; i++) { a[i] = 2 * i; } element = linearSearch( a, arraySize, 10); if( element != -1 ) cout << "Found value in element " << element << endl; else cout << "Value not found." << endl; getch(); return 0; } int linearSearch( const int array[], int length, int value) { if(length==0) return -1; else if (array[length-1]==value) return length-1; else return urch( array, length-1, value); } it is in c++ language ,u can change it to c by including stdio.h ,printf & scanf.....i think it wud be beneficial have a look their too. similar program like that and easy understanding http://fahad-cprogramming.blogspot.com/2014/02/linear-search-program-in-c-using.html
The void keyword is used to show that a method will not return a value. // no return type public void setX(int x) { this.x = x; } // returns an int public int getX() { return x; }
Returning a value of 1 (or any value besides zero) from main indicates an error.
While calling a function, if the arguments supplied are the original values it is called call by value. e.g. int sum(int a, int b) { return (a+b); } int main() { int a=10; int b=20; printf("Sum is %d",sum(a,b)); return 1; }
#include<iostream> int main() { int var=42; // store the value 42 in a variable named var. return(0); }
If you change the return type (to any type no only int) of your 'main' method the jvm no longer can use this method as a entry point for your program. It seems to be no errors, but your program do nothing.
we can call global variables in main method by using scope resolution operator(::) if local and global have same name for example int x=10; main() { int x=20; cout<<x; //x=20 cout<<::x; //x=10 }