1. A method declared as "int" returns an int value.
2. The main() method in Java is not declared as "int", but as "void", meaning it returns no value.
Chat with our AI personalities
* 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; }