Activity 1.3.1.
Run this code to see the output below it. How would you change it to print the ! on the same line as Hi there keeping all 3 print statements?
System.out.println("Hi there!"); prints out the characters between the first " and the second " followed by a new line. The "Hi there!" is called a string literal which is zero to many characters enclosed in starting and ending double quotes. A literal is the code representation of a fixed value, which can be a string or a numerical value.2 + 3 evaluates to 5.+), subtraction (-), and division (/). The multiplication operator is written as *, as it is in most programming languages, since the character sets used until relatively recently didn’t have a character for a real multiplication sign, ×, and keyboards still don’t have a key for it or for ÷. (You may have noticed that + was also used to combine String and other values into new Strings. More on this later.)int or double. An arithmetic expression consisting only of int values will evaluate to an int value. An arithmetic expression that uses at least one double value will evaluate to a double value. This means that when you are doing division with two integers, you will get an integer result and the decimal part of the result will be thrown away. This is called truncating division. If you want a double result, you should make at least one of the values in the expression a double like 2.0.https://scholar.lib.vt.edu/VA-news/ROA-Times/issues/1990/rt9005/900510/05100615.htm*, division /, and remainder % are done before addition + and subtraction -. However, anything in parentheses is done first. It doesn’t hurt to put in extra parentheses if you are unsure as to what will be done first or just to make it more clear.% in Java is the remainder operator. Like the other arithmetic operators is takes two operands. Mathematically it returns the remainder after dividing the first number by the second, using truncating integer division. For instance, 5 % 2 evaluates to 1 since 2 goes into 5 two times with a remainder of 1.2 % 3 and 5 % 2.
% the modulo, or mod, operator. That is not actually correct though the difference between remainder and modulo, which uses Euclidean division instead of truncating integer division, only matters when negative operands are involved and the signs of the operands differ. With positive operands, remainder and mod give the same results. Java does have a method Math.floorMod in the Math class if you need to use modulo instead of remainder, but % is all you need in the AP exam.https://www.youtube.com/watch?v=jp-T9lFISlI&ab_channel=colleenlewis
int and double.+, -, * , /, and % also known as addition, subtraction, multiplication, division, and remainder.int values will evaluate to an int value. With integer division, any decimal part in the result will be thrown away.double value will evaluate to a double value.*, /, % have precedence over + and -, unless parentheses are used to group those to be evaluated first. Operators with the same precedence are evaluated from left to right.ArithmeticException.System.out.print("Java is ");
System.out.println("fun ");
System.out.print("and cool!");
Java is fun and cool!
Java isfun and cool!
Java is fun and cool!
Java is fun and cool!
System.out.println(5 + 5 / 2 * 3 - 1);