Sum of Digits
Logic: 513 -> 5+1+3=9
|
Write a program to find whether no. is palindrome or not.
class Palindrome{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if(reverse == n)
System.out.println(n+" is a Palindrome Number");
else
System.out.println(n+" is not a Palindrome Number");
}
}
/*
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
Write a program to find whether given no. is Armstrong or not.
/*
Example :
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //use to check at last time
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,3);
num = num / 10;
}
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
System.out.println(n+" is not a Armstrong Number");
}
}
Write a program to Display Invert Triangle.
/*
Example:
Input - 5
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
*/
public static void main(String args[]){
Example:
Input - 5
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
*/
class InvertTriangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
while(num > 0){
for(int j=1;j<=num;j++){
System.out.print(" "+num+" ");
}
System.out.print("\n");
num--;
}
}
}
output:: c:> javac InvertTriangle.java
c:> java InvertTriangle 5
5 5 5 5 5
4 4 4 43 3 3
2 2
1
Write a program to generate a Triangle.
/*
eg:
1
2 2
3 3 3
4 4 4 4 and so on as per user given number */
class Triangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
for(int i=1;i<=num;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+i+" ");
}
System.out.print("\n");
}
}
}
eg:
1
2 2
3 3 3
4 4 4 4 and so on as per user given number */
class Triangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
for(int i=1;i<=num;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+i+" ");
}
System.out.print("\n");
}
}
}
Biggest of 3 Numbers using Logical Operators
Program | Output |
//Note: Scanner class work with JDK1.5 or above
|
Please Enter No 1: 5
|
Program For Factorial Numbers
Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1
| |||||||||||||||||||||||||
Fibonacci Series ( 1 1 2 3 5 8 13...)
Logic: Sum of previous two numbers will give us next number.
| ||||||||||||||||||||||||||||||||||
Prime Number
Logic: Prime Number are divisible by itself only.
| ||||||||||||||||
Difference between Post Increment (n++) and Pre Increment (++n)
Post Increment (n++): It increases the value of variable by 1 after execution of the statement. Pre Increment (++n): It increases the value of variable by 1 before execution of the statement.
| ||||||||
Difference between BufferedReader and Scanner class
Even though both BufferedReader and Scanner can read a file or user input from command prompt in Java, there some significant differences between them. One of the main difference between BufferedReader and Scanner class is that former is meant to just read String while later is meant to both read and parse text data into Java primitive type e.g. int, short,float, double, and long. In other words, BufferedRedaer can only read String but Scanner can read both String and other data types like int, float, long, double, float etc. This functional difference drives several other differences on their usage. Another difference is Scanner is newer than BufferedReader, only introduced in Java 5, while BufferedReader is present in Java from JDK 1.1 version. This means, you have access to BufferedReader in almost all JDK version mainly Java 1.4 but Scanner is only available after Java 5. This is also a popular core Java questions from interviews. Since many developer lack Java IO skill, questions like this test their knowledge about API and how to do some practical task.
You will not only learn about those key differences about BufferedReader and Scanner in this article but also about how to use them in Java program. You can also read Core Java Volume 2 - Advanced Features by Cay S. Horstmann to learn more about Java IO fundamentals. It's one of the key area in core Java programming which separate an intermediate Java developer to an expert one.
You will not only learn about those key differences about BufferedReader and Scanner in this article but also about how to use them in Java program. You can also read Core Java Volume 2 - Advanced Features by Cay S. Horstmann to learn more about Java IO fundamentals. It's one of the key area in core Java programming which separate an intermediate Java developer to an expert one.
BufferedReader vs Scanner in Java
Here is the 5 key differences between the Scanner and BufferedReader class of Java API:1. Scanner is a much more powerful utility than BufferedReader. It can parse the user input and read int, short, byte, float, long and double apart from String. On the other hand BufferedReader can only read String in Java.
2. BuffredReader has significantly large buffer (8KB) than Scanner (1KB), which means if you are reading long String from file, you should use BufferedReader but for short input and input other than String, you can use Scanner class.
3. BufferedReader is older than Scanner. It's present in Java from JDK 1.1 onward but Scanner is only introduced in JDK 1.5 release.
4. Scanner uses regular expression to read and parse text input. It can accept custom delimiter and parse text into primitive data type e.g. int, long, short, float or double using nextInt(), nextLong(), nextShort(), nextFloat(), and nextDouble() methods, while BufferedReader can only read and store String using readLine() method.
5. Another major difference between BufferedReader and Scanner class is that BufferedReader is synchronized while Scanner is not. This means, you cannot share Scanner between multiple threads but you can share the BufferedReader object.
This synchronization also makes BufferedReader little bit slower in single thread environment as compared to Scanner, but the speed difference is compensated by Scanner's use of regex, which eventually makes BufferedReader faster for reading String.
Scanner and BufferedReader Example in Java
Though both BufferedReader and Scaner can be used to read a file, Scanner is usually used to read user input and BufferedReader is commonly used to read a file line by line in Java. One reason of this is Scanner's ability to read String, int, float or any other data type and BufferedReader's larger buffer size which can hold big lines from file in memory. Though it's not a restriction and you can even read a file using Scanner in Java. Alternatively, you can even read a file in just one line of code in Java 8.Java Program to use Scanner and BufferedReader
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; /** * Java Program to demonstrate how to use Scanner and BufferedReader class in * Java. * * @author WINDOWS 8 * */ public class ScannerVsBufferedReader{ public static void main(String[] args) { // Using Scanner to read user input Scanner scnr = new Scanner(System.in); System.out.println("======================================="); System.out.println("You can use Scanner to read user input"); System.out.println("======================================="); System.out.println("Please enter a String"); String name = scnr.nextLine(); System.out.println("You have entered " + name); System.out.println("Please enter an integer"); int age = scnr.nextInt(); System.out.println("You have entered " + age); scnr.close(); // Using BufferedReader to read a file System.out.println("======================================="); System.out.println("You can use BufferedReader to read a file"); System.out.println("======================================="); FileReader fileReader; try { fileReader = new FileReader("abc.txt"); BufferedReader buffReader = new BufferedReader(fileReader); System.out.println("File contains following lines"); String line = buffReader.readLine(); while (line != null) { System.out.println(line); line = buffReader.readLine(); } buffReader.close(); fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } Output ======================================= You can use Scanner to read user input ======================================= Please enter a String James You have entered James Please enter an integer 32 You have entered 32 ======================================= You can use BufferedReader to read a file ======================================= File contains following lines 1. Which is best SmartPhone in the market? a) iPhone 6S b) Samsung Galaxy Edge c) Something else
You can see that Scanner is capable of reading both String and numeric data from command line. You can also see how easy it is to read a file line by line using BufferedReader.
Here is a summary of all the differences between Scanner and BufferedReader in Java:
That's all about difference between Scanner and BufferedReader class in Java. Even though, both are capable of reading user input from console, you should use Scanner if input is not big and you also want to read different types of input e.g. int, float and String. Use BufferedReader is you want to read text without parsing. Since it has larger buffer, you can also use to read long String in Java.
Setting of JAVA_HOME and PATH in Linux
The JAVA_HOME environment variable points to the JDK installation directory and used by many Java tools and applications like Eclipse, Maven, ANT, Tomcat etc. to figure out Java executables as they need Java for running. Unfortunately, When you install Java on Linux or Windows, this environment variable is not set automatically. You need to do it yourself. Java developers like me always set useful environment variables like JAVA_HOME, PATH, and CLASSPATH on the login scripts executed by the shell when you log in to Linux e.g. bash_profile.sh, but you can also set JAVA_HOME into /etc/profile for all users. I'll show you steps how to find the location of Java and how to setup the JAVA_HOME in Linux in this article and these steps should also work with any Linux or Java version e.g. Ubuntu, RedHat, CentOS, SUSE, ArchLinux, Debian, Fedora etc.
Steps to set JAVA_HOME in Linux
1) The first step is to find the location where Java is installed on your Linux machine. To check that open putty and connect to your Linux server and type the following command$ java Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
It means Java is installed and added to your PATH, so half of the job is done already. All you need to is find the location of JDK, which you can find either by printing PATH as follows
$ echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/sun/jdk/v1.6.0_16-64bit/bin:.
or
$ which java usr/sun/jdk/v1.6.0_16-64bit
You can see that JDK is installed on "/usr/sun/jdk/v1.6.0_16-64bit".
2) Add the following line of your .bash_profile file if you want to set JAVA_HOME for yourself or /etc/profile if you want to set JAVA_HOME for all user
vim ~/bash_profile export JAVA_HOME=/usr/sun/jdk/v1.6.0_16-64bit/ export PATH = $JAVA_HOME/bin:$PATH
or
vim /etc/profile export JAVA_HOME=/usr/sun/jdk/v1.6.0_16-64bit/ export PATH = $JAVA_HOME/bin:$PATH
That's it you are done. Though don't forget to open another shell window by typing bash, csh, or ksh. The new environment variable will not be added to existing shell, you need to open a new shell window.
Also, if you use bash shell than adding into ~/.bash_profile make sense, if you use csh (C shell) then add JAVA_HOME into ~/.csh_profile and similarly for ksh (K shell) add JAVA_HOME into ~/.ksh_profile.
They are hidden files in your home directory in Linux, that's why they had prefix dot (.) in their name. You can use "ls - alrt" command to see them.
Here is a sample /etc/profile file with JAVA_HOME and PATH variable defined for your reference:
By the way, if you don't see Java installed on your machine, then download the Linux version of Java from Oracle's website and install it. Once you are done with installation just find the folder where JDK is installed and follow the steps given above to set JAVA_HOME for your Linux environment.
For Application, JAVA_HOME is generally referred on start script. It's bad practice to refer hard coded path of Java installation in scripts, you will never find Tomcat, Maven, Eclipse, NetBeans or IntelliJ doing that because then when you update to a new Java version they won't be able to use it until you change their start script. Using JAVA_HOME makes it easy to switch to a different or new Java version. See Core Java for the Impatient to learn more about basic concepts of core Java.
Why should you set JAVA_HOME in Linux
Even though many developers knows about JAVA_HOME they don't set in their environment due to various reasons. I strongly recommend you to set JAVA_HOME, PATH, and CLASSPATH for your development environment. Here are some reasons why you set JAVA_HOME in Linux1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download a new version and make sure your JAVA_HOME points to the new version of Java. This is the best benefit of using an environment variable or links.
2) The JAVA_HOME variable is short and concise instead of the full path to JDK installation directory.
3) JAVA_HOME variable helps your program achieve platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on the respective operating system.
4) The JAVA_HOME is standard, which means other tools which need Java e.g. Maven, Eclipse can refer this variable without having any knowledge of where Java is physically installed on your machine, which is obviously different in different user's machine. JAVA_HOME allows everybody to access Java in a common and standard way.
I also suggest reading Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about Java fundamentals like this one.
That's all about how to set JAVA_HOME environment variable in Linux. I have also touched base on why you should do it. If you face any problem while setting and using the JAVA_HOME environment variable, feel free to ask. I'll be glad to help you out.