Stringbuffer Appendcodepoint Method in Java With Examples: Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

StringBuffer appendCodePoint() Method in Java with Examples

import java.lang.*;

public class Geeks {

public static void main(String[] args)


{

StringBuffer sbf = new StringBuffer("Geeksforgeeks");


System.out.println("String buffer = " + sbf);

// Here it appends the CodePoint as


// String to the string buffer
sbf.appendCodePoint(65);
System.out.println("After appending CodePoint is = " + sbf);
}
}

Output:
String buffer = Geeksforgeeks
After appending CodePoint is = GeeksforgeeksA

There are three types of variables in Java:

 Local Variables
 Instance Variables
 Static Variables

Let us now learn about each one of these variables in detail.

1. Local Variables: A variable defined within a block or method or constructor is


called local variable.

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:

public class StudentDetails {


public void StudentAge()
{
// local variable age
int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}

public static void main(String args[])


{
StudentDetails obj = new StudentDetails();
obj.StudentAge();
}
}

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:

public class StudentDetails {


public void StudentAge()
{ // local variable age
int age = 0;
age = age + 5;
}
public static void main(String args[])
{
// using local variable age outside it's scope
System.out.println("Student age is : " + age);
}
}
Output:
Compilation Error in java code :-
prog.java:12: error: cannot find symbol
System.out.println("Student age is : " + age);
^
symbol: variable age
location: class StudentDetails
1 error

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);

// displaying marks for second object


System.out.println("Marks for second object:");
System.out.println(obj2.engMarks);
System.out.println(obj2.mathsMarks);
System.out.println(obj2.phyMarks);
}
}

Output:
Marks for first object:
50
80
90
Marks for second object:
80
60
85

Static Variables: Static variables are also known as Class variables.

 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 {

// static variable salary


public static double salary;
public static String name = "Harsh";
}

public class EmpDemo {


public static void main(String args[])
{

// accessing static variable without object


Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:"
+ Emp.salary);
}
}

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.

 To create an object of Scanner class, we usually pass the predefined object


System.in, which represents the standard input stream. We may pass an object
of class File if we want to read input from a file.
 To read numerical values of a certain data type XYZ, the function to use is
nextXYZ(). For example, to read a value of type short, we can use nextShort()
 To read strings, we use nextLine().
 To read a single character, we use next().charAt(0). next() function returns the
next token/word in the input as a string and charAt(0) function returns the first
character in that string.

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();

// Print the values to check if the input was correctly obtained.


System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Age: "+age);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("CGPA: "+cgpa);
}
}

Input :

Geek

40

9876543210

9.9

Output :

Name: Geek

Gender: F
Age: 40

Mobile Number: 9876543210

CGPA: 9.9

Java Program to print Fibonacci Series


import java.util.Scanner;

/**
* 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 {

public static void main(String args[]) {

//input to print Fibonacci series upto how many numbers


System.out.println("Enter number upto which Fibonacci series to
print: ");
int number = new Scanner(System.in).nextInt();

System.out.println("Fibonacci series upto " + number +" numbers : ");


//printing Fibonacci series upto number
for(int i=1; i<=number; i++){
System.out.print(fibonacci2(i) +" ");
}

/*
* 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;
}

return fibonacci(number-1) + fibonacci(number -2); //tail recursion


}

/*
* 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++){

//Fibonacci number is sum of previous two Fibonacci number


fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;

}
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

You might also like