Posts

Prime Number Program in Java

Image
Prime Number Program in Java: The prime number is a natural number who value is greater than 1 and can be divisible by 1 and by itself. The number of counts to find the prime numbers is 2 because it can divisible only by 1 and by itself For example: 6 - can be divisible by 1x6,2x3,3x2,6x1. where the count is 4 and it is not a prime number 7 - can be divisible by 1x7,7x1. where the count is 2 and it is a prime number Example 1: Program to check whether the given number is prime number or not import java.util.Scanner; public class Primenumbers { public static void main(String[] args) { int i =2; boolean flag = true; System.out.print("Enter the number to find it is prime or not: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n<0) { System.out.println("The prime number cannot be negative"); } else { while(i<n) { if(n%i==0) { flag = false; break; } i++;

switch statement in java

Image
Switch statement in java: This statement is used to execute one statement among the different statements. In other words, the switch statement is a multiple branched statement similar to if-else-if ladder statement. In switch statement, based on the value it will check with different parts of the code and dispatch the statement. The value of expression can be int,byte,short,char primitive data types and it also accept the enumerated types, wrapper class and string object Important points: The value for the case must same datatypes as the variable in the switch. The value for the case should constants or literals. it cannot be a variable. The value for the case with duplicates is not allowed. The break statement at the end of the each and every case is not mandatory. It is optional. The break statement is used to terminate statement sequence without executing the next case. The default statement in the switch is not mandatory. The case without the break statement continues

StringBuffer methods in java

Image
StringBuffer methods in java: As we know that StringBuffer and StringBuilder are mutable where you can edit / modify without creating the new String object /instance. StringBuffer and StringBuilder provides different methods to be done on String. The methods in StringBuffer and StringBuilder are similiar the only difference is StringBuffer is synchronized and thread safe where StringBuilder is non-synchronized and it is not thread safe. See the Difference between StringBuffer and StringBuilder Syntax: int length() int capacity() void ensureCapacity(int MinimumCapacity) void trimToSize() void setLength(int length) char charAt(int index) int codePointAt(int index) int codePointBefore(int index) int codePointCount(int Beginindex, int Endindex) int offsetByCodePoints(int index, int offsetcodepoint) void getChars(int srcBegin, int srcEnd, char[], int destinationBeginindex) void setCharAt(int index, char ch) StringBuffer delete(int beginIndex, int endIndex) StringBuff

Difference between StringBuffer and StringBuilder

Image
StringBuffer and StringBuilder: The string is immutable class which created the new String object whenever the string is concat or append. The StringBuffer and StringBuilder are mutable class where it will not create the new String object at the time of append / concat instead it just modify the String in same object. Even though StringBuffer and StringBuilder terminology are same, but there is some difference in it which we are going to discuss now. StringBuffer: In StringBuffer every methods are synchronized which means only one thread is allowed to append the string with the cost of performance. It provides thread safety which allows only one thread to perform whereas the other thread has to wait for it's time. java.lang.StringBuffer extends java.lang.AbstractStringBuilder implements java.io.Serializable, java.lang.CharSequence Constructor: StringBuffer() - It will create the empty buffer with initial capacity of 16 StringBuffer(String s) - it will create buffer

Java program to reverse a string

Image
Java program to reverse a string: In this page, you are going to learn how to reverse the string using various methods. The methods are char charAt(),int length(), char[] toCharArray(), byte[] getBytes(), StringBuffer append(). To reverse the words in a sentence String[] split() method is used. Example 1: // using the int length()  and charAt() method import java.util.Scanner; public class Example1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the string: "); String s = sc.nextLine(); System.out.println("Before reversing the string: " + s); String reverse = ""; int n = s.length(); for(int i =n-1; i>=0; i--) { reverse = reverse + s.charAt(i); } System.out.println("After reversing a string: " + reverse); } } Output: Enter the string: Java Program Before reversing the string: Java Program After reversing a string: margorP avaJ

Java program to reverse a number

Image
Java program to reverse a number: In this page, you'll learn how to reverse a number using while loop, for loop, array, recursive method. Example 1: Using the while loop import java.util.Scanner; class Reverse_number { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number: "); int num = sc.nextInt(); int reverse = 0; while(num!=0) { reverse = (reverse*10) + (num%10); num = num/10; } System.out.println("After reversing the number: " + reverse); } } Output: Enter the number: 678 After reversing the number: 876 Example 2: Using the for loop import java.util.Scanner; public class Example2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number: "); int num = sc.nextInt(); int reverse = 0; for(; num!=0; num/=10) { reverse = (reverse*10) + (num%10); }