Another Example

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

No. The + is not allowed as part of an integer. This is weird, and may change with a future release of Java.

Another Example

Here is another example. It asks the user for two integers which are then added together and the sum written out.

import java.util.Scanner;

class AddTwo
{
  public static void main (String[] args) 
  { 
    Scanner scan = new Scanner( System.in );
    int first, second, sum ;      // declaration of int variables

    System.out.println("Enter first  integer:");
    first = scan.nextInt();       // read chars and convert to int

    System.out.println("Enter second integer:");
    second = scan.nextInt();      // read chars and convert to int

    sum = first + second;         // add the two ints, put result in sum

    System.out.println("The sum of " + first + " plus " + second +" is " + sum );
  }
}

Here is a sample run:

Enter first  integer:
12
Enter second integer:
-8
The sum of 12 plus -8 is 4

QUESTION 16:

(A slightly hard question: ) explain what happened in the following run of the same program:

Enter first  integer:
12 -8
Enter second integer:
The sum of 12 plus -8 is 4