Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Okay, so my question is kinda simple, kinda not. The key here is that I need I a

ID: 3548399 • Letter: O

Question

Okay, so my question is kinda simple, kinda not. The key here is that I need I a simple, dummies explanation, in plain, well-spoken English.

Let's say I have some Java expression, like...

b = 'a' + 1;

Let's also assume I don't have access to a compiler so I can't test anything out. I need to know what the type of "b" is. In fact, the above expression doesn't even give 'b' a type--how do I know it's type? In this case, it converts 'a' into an int that's representative of the character and adds '1' to it, so int b = 'a' + 1 = 98.

But how do I know which types take precedence over others, especially when values aren't explicitly typecasted? For instance, if I add a string into the equation, say like

String b = 'a' + 1 + "test";

Well, as you can see, 'b' is a string, and the result is actually "98test", showing that ('a' + 1) was converted into an int and then into characters based on th value itself--woah, why'd it do that?


Pretty much, I need some help determining the type of any Java expression, some way to analyze it and find it for myself without testing--I'll be taking an exam and I won't have computer access at all. Obviously the examples given alone aren't sufficient; I tried searching for some help on this online, but honestly I wasn't even sure what to search, so I didn't have much luck.


So yeah, I need to be able to handle almost anything... ints, doubles, floats, chars, strings, booleans, with various mathematical operators (division, remainder, etc.). And I need it so an idiot like me can understand. That's why what I think should be a very basic principle of Java is going up for 750 points... :

Explanation / Answer

first you need to understand that most of the operators are binary operators there is something known as polish and reverse polish notation for calculating the value of an expression.(If you need a complete understanding read about the above mentioned topics)(There is a specific order in which the values of operators are evaluated)

For the case you've asked the compiler goes from left to right through the expression first it finds 'a'+1 and evaluates it to 98 and then it finds (98)+"test" actually there is a special feature of java

when compared to C this plus operator is overrided as string concatenation.(I hope you know about string concatenation)

The preference order is as below (I've mentioned only arithmetic operators)

('/'=='*')>('+'=='-')

/ has same preference as * ,,, + has same preference as - ,,, both /,* have more preference than both +,-

logically we can't add a number(98) and a string("text") but java compiler automatically converts the number 98 into string "98" and the concatenates "98" and "text"

giving the output as "98text"

let a be a variable and b be another

let ^ denote one of the operators *,/,+,-

if one of a,b is a string the the result is a string s=concat(a,b);

if one of a,b is float the result is float

if one of a,b is double the result is the result is double

if one of a,b is char the output can be taken as both int and char

int n='a'+1;

output

n=98

char c2='b'+1;

char c='a'+1;

char c1='a'+2;

output

c2='c';

c='b';

c1='c';

because b comes just after a in alphabetical order

remember that 'a' and 'A' are different

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote