Explore Java

Be Social. Be Updated.

  • Stumble
  •   
  •   

Swapping variables without temp variable

Before we have seen how to swap two variables with the use of temp. Now, it's time to see how to swap two variables without the use of a temporary variable. This is just easy if you take a look. It is all simple logic. The example below shows that. It is given with explanation of every statement for a better understand.


Illustrating ClassCastException with Example

The following illustrates ClassCastException in Java with example. Consider the example, this seems to be OK but when you execute it.... It is not really. ClassCastException is an unchecked exception i.e. it is not identified at compile time. The ClassCastException is since JDK 1.0



Using Scriptlets in JSP with Example

Using scriptlet in JSP with Example
We may need to write Java code in our JSP program instead of just printing something on the web page. The code usually is not of a single line. So, we need to write multiple lines of code may be 100. We can achieve this with scriptlets. A scriptlet is a fragment of code. Code fragment is written in a scriptlet tag. You can write as many scriptlet tags you want. There is no restriction. You can open a curly brace in one scriptlet tag and close it in the other scriptlet tag. The code fragment is enclosed between <% and %>. Therefore it is obvious that though the code fragments are written in different tags, they are contiguous.



Add two numbers in JSP easily

Adding two numbers
Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Swapping two variables in Java

Swapping two variables is simple
Swapping means inter changing. Swapping two numbers or two variables is nothing but sending the value of one variable to another variable and vice-versa. For instance, consider two variables i=2 and j=4. Now swapping these two variables gives i=4 and j=2. This is what is called as swapping. Now i think, it is obvious for you and that you can do this. Let's see the example.



Get item after the array is used up 'n' times

Get item after array used up n times or by n things.
Sometimes we may encounter a question like, if there are 5 colors and every color is picked up by a student in order and if 72 students pick colors in order one after the other, then what is the color picked by 72th student. For such questions there are answers. In this post, i have written an easy logic that could easily be understood which will outsmart this problem.


About This Blog

This blog contains Java Tutorials, Code Samples,demonstrating several concepts in Java. All the code samples are well tested by a team of experts and hence there will be no programmatical errors. We are seamlessly thriving in making the tutorials easier for beginners.

About the Author

was a software and technology enthusiast. He is a 12th grade student and a part time blogger with extreme love in Java programming.Follow him on Google+ and Twitter or befriend him on Facebook and also on