• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on char & Thread in Marcus's mock

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in Marcus Green Mock Exam 3, Question 3:
int i=2+'2';
it said the above is valid, so i tried the following code for testing.
line 1:int i3 = 1+'1';
line 2: System.out.println("int3 is " + i3);//give 50; since ASCII of char '1' is 49, 49+1 =50
line 3:
line 4: char z = 1;
line 5: System.out.println("char is "+z);// give some symbol that i don't understand, why?
line 6:
line 7: int i4 = 1+z;
line 8: System.out.println("int plus char is "+i4); //give 2 which is expected value, but what is the difference between this and line 1 and line7?
in Marcus Green Mock Exam 2, Question 42:
It said that "A thread can obtain a mutually exclusive lock on a synchronized method of an object" is true.
Why?
Besides, "Java uses a pre-emptive, co-operative system for determining which Thread will execute" - is this statement correct?
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
line 1: int i3 = 1+'1';
line 2: System.out.println("int3 is " + i3);//give 50; since ASCII of char '1' is 49, 49+1 =50
line 3:
line 4: char z = 1;
line 5: System.out.println("char is "+z);// give some symbol that i don't understand, why?
line 6:
line 7: int i4 = 1+z;
line 8: System.out.println("int plus char is "+ i4); //give 2 which is expected value, but what is the difference between this and line 1 and line7?
-> line 5: the first ascii characters can't be displayed, they represent all sorts of things (a carriage return / a line feed / a bell / etc.), so they look strange on the display.
-> in line 8: the variable z is converted to int; as you assigned it with the value 1, it is converted to int value 1, and added to int value 1, it gives 2
-> in line 1 you add the int value 1 with the character literal '1', this char. literal is also converted, and gives int value 49, so the addition results in 50 -> that the difference
greetings,
TK
 
Thomas Kijftenbelt
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
regarding your second question:
the thread can obtain a lock on the object, by entering a piece of synchronized code; as soon as this happens, the thread gets a lock on the object, and no other thread can enter the synchronized method of this object.

Besides, "Java uses a pre-emptive, co-operative system for determining which Thread will execute" - is this statement correct?


I would say yes, this is a definition of the thread scheduler.
Greetings,
TK
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Besides, "Java uses a pre-emptive, co-operative system for determining which Thread will execute" - is this statement correct? "

This is not a quote from me, the scheduling algorithm used by Java threads is platform dependent.
Marcus
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Green:
"Besides, "Java uses a pre-emptive, co-operative system for determining which Thread will execute" - is this statement correct? "



Hum, this is false.
It was pre emptive in the really beginning, and even so, as Marcus said, it is JVM dependent, so you cannot say that java is pre emptive or time sliced.
 
reply
    Bookmark Topic Watch Topic
  • New Topic