Sri Karr

Greenhorn
+ Follow
since Mar 19, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sri Karr

you could also try the below just to avoid little bit extra coding
14 years ago
JSP
Please use the variable "counter" to iterate over the inner c:forEach loop and use that to find out even and odd rows.


</c:forEach>
</table>
[/code]
14 years ago
JSP
Have you looked at your web.xml file in your application.
I hope the below information will resolve your problem.

http://faq.javaranch.com/java/SetupJstlForJsp2

How do I declare the web.xml file for Servlets 2.5 and JSP 2.1? (Tomcat 6.0.14 and so on)

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>web app name here</display-name>

<!-- rest of declarations go here -->

</web-app>
How do I declare the web.xml file for Servlets 2.4 and JSP 2.0? (Tomcat 5, Resin 3, and so on)


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>web app name here</display-name>

<!-- rest of declarations go here -->

</web-app>
You just need to know the variable declaration rules, for loop rules to understand the above problem.
Assume that we need to declare two variables, i, j of int type..we do like this
int i=10;
int j=20; ( please keep it in mind that we separate the declaration of variables using ; symbol)
(or) int i=10;int j=20; ( can do on the same line)
(or) int i=10,j=20; ( in this case we are saying that i and j are of type int and separating them with , (comma) )


if i and j are of different type we do,
int i=10; double j=20.0; (or)
int i=10;
double j=20.0;
but can not do like this int i=10,double j=20; ( the compiler complains about --should be separated with ; not , )

coming to for loop ;;the syntax is
for ( exp1 ; exp2;exp3) { }

; is used to separate the expressions declared in the for loop;

when we are using exp1 for declaring variables we need to understand how to declare or initialize variables.
if I use

int i;
for(i, int j; i<=10; i++)
the compiler asks you to separate the "i, int j" with semicolon, since i is already declared as int in the scope ( declaration can happen only in exp1(one time) and after that whatever is specified should be separated with semicolon but if you do that for loop is not valid ( for(i;intj;i<=10;i++) )

but you can use

for(int i=10,j=20 ; i<=20;i++) {}

Since the first expression in the for loop 'int i=10,j=20' is treated as if we are declaring i and j variables of both type int.

I hope this will help you understand the above problem
There can be a question about its usage, like can you use volatile for a method? or they can give an example of code which uses Volatile in front of a method declaration and asking the exam taker as why this code can not compile.

Just know that volatile is used for Variables not for Methods or classes.
What does volatile do?

This is probably best explained by comparing the effects that volatile and synchronized have on a method. volatile is a field modifier, while synchronized modifies code blocks and methods. So we can specify three variations of a simple accessor using those two keywords:

int i1; int geti1() {return i1;}
volatile int i2; int geti2() {return i2;}
int i3; synchronized int geti3() {return i3;}

geti1() accesses the value currently stored in i1 in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it's thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory. So in fact, it is possible for the "main" memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1 and for thread2 to have a value of 3 for i1 if thread1 and thread2 have both updated i1 but those updated value has not yet been propagated to "main" memory or other threads.

On the other hand, geti2() effectively accesses the value of i2 from "main" memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile must have it's data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that volatile variables have a higher access and update overhead than "plain" variables, since the reason threads can have their own copy of data is for better efficiency.

Well if volatile already synchronizes data across threads, what is synchronized for? Well there are two differences. Firstly synchronized obtains and releases locks on monitors which can force only one thread at a time to execute a code block, if both threads use the same monitor (effectively the same object lock). That's the fairly well known aspect to synchronized. But synchronized also synchronizes memory. In fact synchronized synchronizes the whole of thread memory with "main" memory. So executing geti3() does the following:

1. The thread acquires the lock on the monitor for object this (assuming the monitor is unlocked, otherwise the thread waits until the monitor is unlocked).
2. The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory (JVMs can use dirty sets to optimize this so that only "dirty" variables are flushed, but conceptually this is the same. See section 17.9 of the Java language specification).
3. The code block is executed (in this case setting the return value to the current value of i3, which may have just been reset from "main" memory).
4. (Any changes to variables would normally now be written out to "main" memory, but for geti3() we have no changes.)
5. The thread releases the lock on the monitor for object this.

So where volatile only synchronizes the value of one variable between thread memory and "main" memory, synchronized synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.

http://www.javaperformancetuning.com/news/qotm030.shtml
Math.round is a method which accepts one argument of two different types(double/float).There is no empty parameter method defined for Math.round, Since this method is defined to round up some number, which need to be passed to it.
1) static long round(double a)
Returns the closest long to the argument.
2) static int round(float a)
Returns the closest int to the argument.

In the example, when you are using Math.round(value[x]+0.5) inside the for loop, while iterating through the values array you are just passing values[0] which is -2.3, value[1] which is -1.0 ..values[3] to the Math.round funtion. ( Math.round( -2.3+0.5), Math(-1.0+0.5).....etc

Please try the following to understand the output.

for(int x=0;x<values.length;x++){
long j=Math.round(values[x]+.5);
double k=Math.ceil(values[x]);
System.out.println("j :"+j);
System.out.println("k :"+k);
if(Math.round(values[x]+.5) ==(Math.ceil(values[x]))){
++cnt;
}
}
System.out.println("Same results "+cnt+" times(s)");

output:
j :-2
k :-2.0
j :0
k :-1.0
j :1
k :1.0
j :5
k :4.0
Same results 2 times(s)
( cnt value is updated when j and k values are same, in this case j=-2 and k=-2.0 is same and j=1 and j=1.0 is same)
You do not need to worry about the exact answer for this. As the exam doesn't test our mathematical skills.
We knew that when we convert a large integer (which has the value out of its range -32768 to 32767)to float the value is going to change slightly.And when we convert the float to integer for the same it is going to loose its precision.(Narrowing conversion).

When we look at the answers for the above question:

1) It will print 0.


2) It will not print 0.


3) It will not compile.


4) It will throw an exception at runtime.


5) None of the above.

we knew that definitely it won't print "0" as the number is large, the conversion changes the number slightly, eliminate 1st option, the code is perfectly fine, so eliminate 3 and 4 th options. You need to choose from options 2nd and 5th.

You knew that if code compiles and doen't throw runtime exception, it will print some number and in this case it won't be "0".so please select 2nd as your answer.
(I guess this question is kind of logical thinking question rather than pure technical questions).
Try the following:
int i = 1234567890;
float f = i;
System.out.println(i);
System.out.println(f);
System.out.println((int)f);

output: 1234567890
1.23456794E9
1234567936

When you do System.out.println(i-(int)f) it will print -46.Since the value of the integer falls out of the range of Integers, it looses its precision from widening and shortening conversions.