Stringbuffer Appendcodepoint Method in Java With Examples: Output
Stringbuffer Appendcodepoint Method in Java With Examples: Output
Stringbuffer Appendcodepoint Method in Java With Examples: Output
import java.lang.*;
Output:
String buffer = Geeksforgeeks
After appending CodePoint is = GeeksforgeeksA
Local Variables
Instance Variables
Static Variables
o These variable are created when the block in entered or the function is
called and destroyed after exiting from the block or when the call returns
from the function.
o The scope of these variables exists only within the block in which the
variable is declared. i.e. we can access these variable only within that
block.
o Initilisation of Local Variable is Mandatory.
Sample Program 1:
Output:
Student age is : 5
In the above program, the variable age is a local variable to the function StudentAge(). If
we use the variable age outside StudentAge() function, the compiler will produce an
error as shown in below program.
Sample Program 2:
Instance Variables: Instance variables are non-static variables and are declared in a
class outside any method, constructor or block.
As instance variables are declared in a class, these variables are created when
an object of the class is created and destroyed when the object is destroyed.
Unlike local variables, we may use access specifiers for instance variables. If we
do not specify any access specifier then the default access specifier will be used.
Initilisation of Instance Variable is not Mandatory. Its default value is 0
Instance Variable can be accessed only by creating objects.
Sample Program:
import java.io.*;
class Marks {
// These variables are instance variables.
// These variables are in a class
// and are not inside any function
int engMarks;
int mathsMarks;
int ph
yMarks;
}
class MarksDemo {
public static void main(String args[])
{
// first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;
// second object
Marks obj2 = new Marks();
obj2.engMarks = 80;
obj2.mathsMarks = 60;
obj2.phyMarks = 85;
// displaying marks for first object
System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);
Output:
Marks for first object:
50
80
90
Marks for second object:
80
60
85
These variables are declared similarly as instance variables, the difference is that
static variables are declared using the static keyword within a class outside any
method constructor or block.
Unlike instance variables, we can only have one copy of a static variable per
class irrespective of how many objects we create.
Static variables are created at the start of program execution and destroyed
automatically when execution ends.
Initilisation of Static Variable is not Mandatory. Its default value is 0
If we access the static variable like Instance variable (through an object), the
compiler will show the warning message and it won't halt the program. The
compiler will replace the object name to class name automatically.
If we access the static variable without the class name, Compiler will
automatically append the class name.
To access static variables, we need not create an object of that class, we can simply
access the variable as
class_name.variable_name;
import java.io.*;
class Emp {
Output:
Harsh's average salary:1000.0
class Test
{
// We can initialize here, but if we
// initialize here, then all objects get
// the same value. So we use blank final
final int i;
Test(int x)
{
// Since we have initialized above, we
// must initialize i in constructor.
// If we remove this line, we get compiler
// error.
i = x;
}
}
// Driver Code
class Main
{
public static void main(String args[])
{
Test t1 = new Test(10);
System.out.println(t1.i);
Test t2 = new Test(20);
System.out.println(t2.i);
}
}
9GeeksforGeeks
GeeksforGeeks2016
8GeeksforGeeks2016
8GeeksforGeeks9
Scanner is a class in java.util package used for obtaining the input of the primitive types
like int, double, etc. and strings. It is the easiest way to read input in a Java program,
though not very efficient if you want an input method for scenarios where time is a
constraint like in competitive programming.
Let us look at the code snippet to read data of various data types.
import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
String name = sc.nextLine();
// Character input
char gender = sc.next().charAt(0);
// Numerical data input// Java program to read data of various types using Scanner class.
import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
String name = sc.nextLine();
// Character input
char gender = sc.next(
// byte, short and float can be read
// using similar-named functions.
int age = sc.nextInt();
long mobileNo = sc.nextLong();
double cgpa = sc.nextDouble();
Input :
Geek
40
9876543210
9.9
Output :
Name: Geek
Gender: F
Age: 40
CGPA: 9.9
/**
* Java program to calculate and print Fibonacci number using both recursion
* and Iteration.
* Fibonacci number is sum of previous two Fibonacci numbers fn= fn-1+ fn-2
* first 10 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*
* @author Javin
*/
public class FibonacciCalculator {
/*
* Java program for Fibonacci number using recursion.
* This program uses tail recursion to calculate Fibonacci number for a
given number
* @return Fibonacci number
*/
public static int fibonacci(int number){
if(number == 1 || number == 2){
return 1;
}
/*
* Java program to calculate Fibonacci number using loop or Iteration.
* @return Fibonacci number
*/
public static int fibonacci2(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
}
return fibonacci; //Fibonacci number
}
}
Output:
Enter number upto which Fibonacci series to print:
12
Fibonacci series upto 12 numbers :
1 1 2 3 5 8 13 21 34 55 89 144