Syntax
public class Main {
access_modifier return_type name_of_method(){
// code
return statement;
}
}
Example
public class Main(){
static void myMethod(){
System.out.print("hi");
}
}
public static void main(){
myMethod(); // calling the function
}
This method myMethod() does not have a return value
Example
public static void main(){
String name = "java";
}
static void greet(int naam){
System.out.print(naam);
}
Here copy of 'name' is created and passed to 'greet' function
i.e. passing value of the reference
Example 2
public static void main(){
String name = "a";
change(name);
System.out.print(name);
}
static void change(String naam){
naam = "b"; // not changing original object, just creating new object.
}
Points to be noted
Example 1
public static void main(){
int a = 10;
int b = 20;
swap(a,b);
// after function call the original value of 'a' and 'b' is not changed
}
static void swap(int num1,int num2){
int temp = num1;
num1 = num2;
num2 = temp; // at function scope level they are swapped
}
Example 2
int arr[]=[1,2,3,4,5];
nums = arr;
nums[0]=99;
Variables declared inside a method/function scope (means inside method) can't be accessed outside the method.
Example
public static void main(){
int x = 4;
}
static void fun(){
System.out.print(x); // can't be accessed outside main function
}
Example
public static void main(){
int a = 10;
int b = 20;
{
int a = 5; // Error - Can't redeclare
a = 100; // can update
int c = 20;
}
c = 5; // can't access block variable
int c = 15; // can declare another once
a = 50;
}
Variables declared inside loop are having loop scope.
In is the practive of using variables in overlapping scopes with the same name where the variable in low-level scope overrides the variable of high-level scope. Here the variable at high-level scope is shadowed by low-level scope variable.
Example
public class Shadowing(){
static int x = 90;
public static void main(){
System.out.println(x); // x = 90
int x = 50; // here high-level scope is shadowed by low-level scope
System.out.println(x); // x = 50
}
}
Variable arguments is used to take a variable number of arguments.
A method that takes a variable number of arguments is a var-args method
Syntax
public static void main(){
fun(1,2,4,5,6,7,4,5,3); // can passed any number of variables
multiArg(3, "navy" ,234,5,4,3,5,34,4)
}
static void fun(int ...a){
// method body
}
static void multiArg(int a, String b, int ...v){
System.out.println(a + " " + b + " " + Arrays.toString(v));
}
Function Overloading happens when two functions have same name.
Example 1
static void fun(){
// code
}
static void fun(){
// code
}
// This will gives ERROR
Example 2
static void fun(int a){
// code
}
static void fun(int a, int b){
// code
}
// The ERROR is resolved - as both are having different arguments with same method name.
At complie time, it decides which function to run.
An Armstrong number is a positive m-digit number that is equal to the sum of the mth powers of
their digits. It is also known as pluperfect, or Plus Perfect, or Narcissistic number. It is an
OEIS sequence A005188. Let’s understand it through an example.
Armstrong Number Example
1: 11 = 1
2: 21 = 2
3: 31 = 3
153: 13 + 53 + 33 = 1 + 125+ 27 = 153
125: 13 + 23 + 53 = 1 + 8 + 125 = 134 (Not an Armstrong Number)
1634: + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1643
public class Questions {
public static void main(String[] args) {
printArm();
}
// print every three digit armstrong numbers
static void printArm(){
for (int i = 100; i < 1000; i++) {
int isArm=i;
int sum=0;
while(isArm>0){
int last =isArm%10;
sum =sum+ last*last*last;
isArm/=10;
}
if(sum==i){
System.out.println(i+ " is a Armstrong number");
}
}
}
}