690 likes | 868 Views
Overview of java principle . Chapter 2. Topics . 1.Brief History of Java 2.What is Java? 3.Differences to C++ 4.Java Development Tools 5.The First Simple Program 6.Structure of a Class 7.Variables 7.1.Declaration 7.2.Scope 7.3Constants 8.Data Types 8.1.Primitive Data Types
E N D
Overview of java principle Comp321: Object Oriented Programming Chapter 2
Topics 1.Brief History of Java 2.What is Java? 3.Differences to C++ 4.Java Development Tools 5.The First Simple Program 6.Structure of a Class 7.Variables 7.1.Declaration 7.2.Scope 7.3Constants 8.Data Types 8.1.Primitive Data Types 8.2.Reference Data Types 9.Operators 9.1.Arithmetic 9.2.Comparison and Conditional 9.3.Assignment 9.4.Others 10.Type Conversions and Casting 11.Mathematical Functions Comp321: Object Oriented Programming
1.Brief History of Java • Java was originally developed by Sun Microsystems, a US company, in 1991. Its first name was Oak and it was designed for the development of software for consumer electronic devices such as televisions and video recorders. As such, one of the main goals for the language was that it is simple, portable and highly reliable. • The team based the new language on the existing C and C++ languages, but they removed features of C and C++ that were seen as being the sources of problems. • In 1993, the WWW (World Wide Web) was starting to be used, transforming the previously text-based internet into a more graphic-rich environment. The team developing the Oak language came up with the idea of creating small programs, called applets, that would be able to run on any computer that was connected to the internet. Comp321: Object Oriented Programming
…cont • In 1995, Oak was renamed Java due to some legal issues – the name does not have any particular meaning. Java was supported by many IT companies, including the big internet browser developers, Netscape and Microsoft. • Since then, Java has become one of the main languages used for internet programming and also as a general-purpose object-oriented language for stand-alone applications. Comp321: Object Oriented Programming
2.What is Java? • Java technology consists of a programming language and a platform on which programmes can be run. • Java is different from other programming languages because a Java program is both compiled and interpreted but in many other languages, the program is either compiled or interpreted. • The Java language is object-oriented and programs written in Java are portable – that is, they can be run on many different platforms (e.g. Windows, Mac, Linux, Solaris). • Mac, short for Macintosh, is an operating system owned by the Apple Corporation. The Mac OS has a GUI that looks like Windows but has a different underlying architecture. It is a competitor to Windows. The Mac OS has to be run on Apple Mac computers, which, again, have a different underlying architecture to PCs like IBM, Acer etc. Comp321: Object Oriented Programming
...cont • Solaris is a platform from Sun Microsystems. Like Windows, Solaris has server and workstation operating systems. • The Java compiler translates the program into an intermediate language called Java bytecodes. • Java bytecodes are platform-independent – this means they can be run on different operating systems (e.g. Windows and Mac). • There are different Java interpreters for different platforms. The interpreter parses and runs each of the Java bytecode instructions. Comp321: Object Oriented Programming
Compiling and Interpreting of a Java program Comp321: Object Oriented Programming
Compiling and Interpreting of a Java program • Compilation only needs to happen one time, whereas interpretation happens every time the program is executed. • The source code for a Java program is saved to one or more .java files. • The compiler generates a .class file for each .java file. • A .java file can be opened and viewed in any text editor, but a .class file is bytecode so it cannot be viewed in the same way. • Every Java interpreter is actually an implementation of something called the Java VM (Virtual Machine). • It is called a Virtual Machine because it is like a computer that does not physically exist • The VM exists only within the memory of the computer. The Java bytecodes are like a set of machine code instructions for the Java VM. A web browser that is Java-enabled is an example of an interpreter. Comp321: Object Oriented Programming
…cont • The big benefit of the Java way of doing things is that the program code can be written once, compiled and then run on many different platforms. • See the figure below for an illustration of this concept. There is one Java VM, but many different implementations of it – i.e. many different interpreters for the Java bytecodes. Comp321: Object Oriented Programming
…cont • Figure - a Java program can be compiled once and then run on different platforms Comp321: Object Oriented Programming
…cont • As mentioned above, Java includes a programming language and a platform. • A platform refers to the hardware (PCs, servers, printers etc) and software (operating systems, application software etc) in which a program is run. So, for example, the platform we use here in your computer laboratory is Windows XP/7 running on IBM-compatible PCs. • The Java platform is a bit different to other platforms, in that it consists of software only that means it is hardware-independent. This is what makes it possible to run compiled Java programs on different platforms. • The Java platform consists of: • The Java VM • The Java API (Application Programming Interface). Comp321: Object Oriented Programming
…cont • The API provides libraries of pre-written, ready-to-use components that provide standard functionality. • A library groups a number of related classes and interfaces together into what is called a Java package. You can choose what packages you want to use in your program. • In order to write Java programs, you need to have the Java SDK (Software Development Kit) installed on your PC. This includes the API and the VM, as well as compilers and debuggers for Java. • Java is a programming language which is based on the OOP (object-oriented paradigm). • 'Paradigm' means a set of ideas, a concept or hypothesis. • You should already be familiar with some OO concepts from your study of C++. However, there are some differences between Java and C++. Comp321: Object Oriented Programming
3.Differences to C++ • Java is a true OO language, while C++ is C with an object-oriented extension (C is not an OO language). • The following is a list of some major C++ features that were intentionally omitted from Java or significantly modified. You may not have covered all of these C++ features in your C++ programming course, but you should be familiar with some of them. • There are, of course, other differences between Java and C++, which you will discover as we go through the course. Comp321: Object Oriented Programming
...cont • The differences are: • Java does not support operator overloading . • Java does not have template classes. • Java does not directly support multiple inheritance of classes, but it does allow this to be achieved using a feature called an interface. • Java does not support global variables – every variable and method is declared within a call and forms part of that class (this relates to the scope of variables. • Java does not use pointers (but similar functionality is achieved using implicit references to objects). • Java has replaced the destructor function with a finalize() function. • There are no header files in Java (but there is a way to import all the classes from another package or library). Comp321: Object Oriented Programming
4.Java Development Tools • To get started with writing Java programs, you need to have the Java SDK installed on your PC. • The current version of the Java itself is Java 2; the current version of the SDK is 6.9. The latest version of the SDK can be downloaded for free from the Sun web site (http://java.sun.com). • The SDK provides various tools for working with Java code, as well as the Java VM and the API. • Some of the tools are: • Javac (Java compiler) • Java (Java interpreter i.e. to run a Java program) • Javadoc (for creating HTML-format documentation from Java source code) • Jdb (Java Debugger) Comp321: Object Oriented Programming
...cont • The Java API consists of many built-in classes and methods that you can use in your code. This reflects the fact that the Java technology is built using Java itself. • Some of the most commonly used packages are the following: • Language support package (java.lang) – classes required for implementing basic features of Java. • Utilities package (java.util) – classes that provide various utility functions such as date and time functions. • Input/Output package (java.io) – classes required for manipulation of input/output to/from programs • Networking Package (java.net) – classes for communicating with other programs/PCs over networks and internet • Applet Package (java.applet) – classes that are used to create Java applets. Comp321: Object Oriented Programming
5.The first simple Program 1. /* This is a simple Java program. */ 2. class Example { 3. // Your program begins with a call to main(). 4. publicstaticvoid main(Stringargs[]) { 5. System.out.println(“Hello comps!"); 6. } 7. } • When the program is run, the following output is displayed: Hello comps! Comp321: Object Oriented Programming
…cont • Line 1: is multiple line comment which is like in C++. /*multiple line comment*/ • Line 2: class Example { • This line uses the keyword class to declare that a new class is being defined. • Example is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}). • The use of the curly braces in Java is identical to the way they are used in C, C++, and C#. • For the moment, don’t worry too much about the details of a class except to note that in Java, all program activity occurs within one. This is one reason why all Java programs are (at least a little bit) object-oriented. • Line 3: is the single-line comment which is like in C++. // single line comment Comp321: Object Oriented Programming
…cont • Line 4: public static void main(String args[]) { • This line begins the main( ) method. As the comment preceding it suggests, this is the line at which the program will begin executing. All Java applications begin execution by calling main( ). (This is just like C/C++.) • The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. (The opposite of public is private, which prevents a member from being used by code defined outside of its class.) • In this case, main( ) must be declared as public, since it must be calledby code outside of its class when the program is started. Comp321: Object Oriented Programming
…cont • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. • The keyword void simply tells the compiler that main() does not return a value. • In main( ), there is only one parameter, albeit a complicated one. String args[ ] declares a parameter namedargs, which is an array of instances of the class String. (Arrays are collections of similar objects.) Objects of type String store character strings. • In this case, argsreceives anycommand-line argumentspresent when the program is executed. This program does not make use of this information, but other programs which we’ll see later will do. Comp321: Object Oriented Programming
…cont • Line:5 System.out.println(“Hello Comps!"); • This line outputs the string “Hello Comps!” followed by a new line on the screen. • Output is actually accomplished by the built-in println() method. In this case, println( ) displays the string which is passed to it. • As you will see, println( ) canbe used to display other types of information, too. • The line begins with System.out. - System is a predefined class that provides access to the system, and out is the output stream that is connected to the console Comp321: Object Oriented Programming
6.Structure of a Class • All Java code is written inside classes. • The class definition is the first part of the code that appears. This consists of the access modifier for the class (public or private), the class keyword and the name of the class. • By convention, class names begin with an upper case letter.For example, to define a class called Circle : public class Circle { } Comp321: Object Oriented Programming
…cont • Inside the class, the code can be divided into fields, constructors and methods. • The fields are the data members of the class. The data members can be class variables or instance variables. • The constructors are special methods that are called when an object is instantiated from the class. There can be more than one constructor for a class – as long as each constructor has a different parameter list. For example, in a Circle class, the radius for a new object could be passed to the constructor, or if no radius is passed, a default value could be assigned for the radius. In fact, this also applies to methods. This feature of Java is called method overloading. Comp321: Object Oriented Programming
…cont • The methods implement the behaviour of the objects belonging to the class. Generally, methods return information about the state of an object or they change the state of an object. These are sometimes called accessor and mutator methods. • The order of these parts of a class is generally not important, but placing the fields and then the constructors at the beginning does make the class readable and easy to get around for programmers. Of course, comment blocks should also be used to document the code. Comp321: Object Oriented Programming
…cont (Eg) public class Circle { //data private double radius; //constructors public void Circle () { radius = 1.0;} public void Circle (double r) { radius = r; } //methods public double calcArea() { return 3.14* radius * radius; } public void draw() { … … } } Comp321: Object Oriented Programming
7.1.Variables Declaration • Remember that a variable is like a storage location to store a data value and that a variable can take different values at different times during the execution of a program. • Some Java variable declarations include: int x; FirstJavamyObject = new FirstJava (); int sum = 0; • A variable must be given an explicit name and data type. • The name must be a legal identifier (an identifier is simply a name given to an item used in a Java program). A legal identifier begins with a letter and can consist of alpha-numeric characters (letters and digits) and can also include the underscore (_) and dollar ($) characters. Identifiers are case-sensitive and cannot have spaces in them. • Some data types have default values that are used for initialisation if a variable is not explicitly initialised in code. For example, the integer data type has a default value of 0. Comp321: Object Oriented Programming
7.1.Variables Declaration(cont) • A convention used in Java programming is that variable names begin with a lowercase letter while class names begin with an uppercase letter. If a variable or class name has more than one word in it, the words are joined together and the first letter of each word after the first will begin with a capital letter. This convention makes code easier to read and understand. • A variable declaration is used to give a variable a name and a data type – the format is to put the type followed by the name i.e. type name • A variable must be initialised before it is used (the compiler will not allow an uninitialized variable to be used). Initialisation simply means assigning some initial value to a variable e.g. • inti ; • i = 0; //to initialise an integer variable to the value 0. • However, declaration and initialisation are often combined into one statement e.g. • inti = 0; Comp321: Object Oriented Programming
7.2.Variables Scope • A variable has scope. • Scope refers to the parts of the code in which the variable name can be used without prefixing it with the class or object name. The scope is determined by where in the class definition a variable is declared. • Generally, Java variables have either class scope or local scope. • Static variables or fields , which is another way of saying 'class variable’ have class scope – because they can be referenced within the class and its subclasses without prefixing with the class name. • Instance variables are variables that belong to instances (objects) of a class. These also have class scope, because they must be prefixed with the instance name when used outside the class or its subclasses. Comp321: Object Oriented Programming
7.2.Variables Scope(cont) • The difference between static, or class, variables and instance variables is that an instance variable can take different values for each object while a class variable has the same value for all objects of that class. • Class and instance variables are declared at the class level – not within methods. But they can be used within methods in the class, without prefixing with the class or instance name. • Variables that are declared within a method or within a block of code have local or lexical scope, that is, they can be used only within the enclosing block of statements or method. For example, in the following block of code, the variable i is out of scope in the line that begins 'System.out.println' because it was declared within the block of statements following the if statement, so its scope is that block. Thus the last line will not compile: Comp321: Object Oriented Programming
7.2.Variables Scope(cont) • if (...) { inti = 17; ...} System.out.println("The value of i = " + i); // error • The scope of parameters to a method or cannot be used outside the method or constructor. • Local variables do not have the public or private modifiers in front of them. The lifetime of a local variable is only the time of the method execution – they are created when a method is called and destroyed when the method finishes. To summarise: • Class variables are used to store data that is common to all objects of the class. • Instance variables are used to store data that persists through the lifetime of an object. • Local variables are often used as temporary storage locations while a method or constructor completes its task. Comp321: Object Oriented Programming
7.3.Variables Constants • In Java, a variable declaration can begin with the finalkeyword. This means that once an initial value is specified for the variable, that value is never allowed to change. • This is the equivalent of a constant in C++ or other languages. • For example, to declare a constant for the value of the maximum number of students allowed on a course: finalint MAX_STUDENTS = 100; • The variable can be initialised after the declaration – but if the declaration includes the final modifier, the initial value is the last value that can be assigned. finalint MAX_STUDENTS; MAX_STUDENTS = 100; • Note that the convention in Java programming is to use all uppercase letters for constant names, so that they stand out in the code. Underscores are used to separate the words if there is more than one word in the constant's name. Comp321: Object Oriented Programming
8.Data Types • Every variable must have a data type – the data type determines the values that a variable can contain and also what operations can be performed on it. • Java has two types of data type – • primitive and • reference. Comp321: Object Oriented Programming
8.1.Primitive Data Type • A variable of primitivetype contains a single value of the appropriate size and format for its type: a number, a character, or a boolean value. For example, an integer value is 32 bits of data in a format known as two's complement, the value of a char is 16 bits of data formatted as a Unicode character, and so on. • A variable of referencetype has as its value an address, or a pointer to the values or set of values that the variable represents. Classes, interfaces and arrays are reference types. • The primitive types can be divided into numeric and non-numeric types. • Numeric types can further be divided into integer and real number (or floating-point) types. Integer types hold whole numbers, positive and negative. Real number types can hold decimal values e.g. 1.234, -6.754. Comp321: Object Oriented Programming
Numeric data types in Java Comp321: Object Oriented Programming
…cont • The bigger storage size types take up more memory (i.e. short takes more memory than byte). So, you should select a data type appropriate to the values that will be held in a variable e.g. if the values will be in the range –30000 to +30000, use short rather than int. All Java number values are signed – which means they can be positive or negative. • Non-numeric data types are boolean and character. • The boolean type has only two possible values, representing the two Boolean states – true and false. Java reserves the words true and false for these values. • Comparison operators (e.g. >, <, = =) all return boolean type values. • Unlike C/C++, boolean values in Java cannot be converted to or from other data types e.g. 0 and 1 cannot be converted to true and false. • The character type, called char in Java, represents any Unicode character. The char type takes up 16 bits (2 bytes) in memory and holds only a single character. • Java provides some built-in classes, such as String and StringBuffer, for working with strings of char values. Comp321: Object Oriented Programming
…cont • Literal primitive values can be used directly in code e.g. char myChar = 'A'; //to assign the character A to a char variable intmyInt = 5; // to assign the value 5 to an integer variable • The declarations above also show how a value can be assigned to a variable as part of the variable declaration e.g. the myChar variable is initially given the value of 'A'. • For the char type, there are escape sequences for special characters e.g. \b for a backspace, \n for a newline, \t for a tab, \\ for a backslash, \', \“. • Generally speaking, a series of digits with no decimal point is typed as an integer. A long integer can be specified by putting an 'L' or 'l' after the number. 'L' is preferred as it cannot be confused with the digit '1'. • A series of digits with a decimal point is of type double (e.g. 34.543). • But you can specify that the number is a float by putting an 'f' or 'F' after the number (e.g. 34.543f). Comp321: Object Oriented Programming
…cont • A literal character value is any single Unicode character b/n single quote marks. The two boolean literals are simply true and false. • Java has wrapper classes for each of the integer types – these are Byte, Short, Integer and Long. These classes define MIN_VALUE and MAX_VALUE constants that describe the range of each type. They also have useful static methods that can be used to convert strings to integer values e.g. Integer.parseInt() to convert a string to an integer. The exercise below demos these wrapper classes. • The primitive types have default values – so if a variable is not explicitly initialised, it is automatically initialised to its corresponding default value. These are 0 for int, 0.0 for floating-point values, and an empty string for char. Comp321: Object Oriented Programming
* Uses the wrapper classes to get the min and max values for each number type. */ public class MaxVariablesDemo { public static void main(String args[]) { // integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; intlargestInteger = integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; double largestDouble=Double.MAX_VALUE; // other primitive types char aChar = 'S'; booleanaBoolean = true; // display them all System.out.println("The largest byte value is" + largestByte); System.out.println("The largest short value is " + largestShort); System.out.println("The largest integer value is " + largestInteger); System.out.println("The largest long value is " + largestLong); System.out.println("The largest float value is " + largestFloat); System.out.println("The largest double value is " + largestDouble); if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); } System.out.println("The value of aBoolean is " + aBoolean); } } Comp321: Object Oriented Programming
The output will be The largest byte value is127 The largest short value is 32767 The largest short value is 2147483647 The largest float value is 9223372036854775807 The largest float value is 3.4028235E38 The largest float value is 1.7976931348623157E308 The character S is upper case. The value of aBoolean is true Comp321: Object Oriented Programming
8.2.Reference Data Types • Arrays and classes are reference data types in Java. • While a variable that has a primitive data type holds exactly one value, a variable that has a reference data type is a reference to the value or set of values represented by the variable. Classes • When a class is defined, it can then be used as a data type. The variable declaration for a reference data type is the same as for a primitive data type. For example, if a program has a class named Customer, a new variable, myCustomer1, to hold an object of type Customer can be declared like this: Customer myCustomer1; Comp321: Object Oriented Programming
Cont… • However, to assign a reference to the variable, the new keyword must be used – new creates a new object from the specified class. The class name is followed by parentheses in which any arguments required by the class constructor are passed e.g. Customer myCustomer1 = new Customer(); • The above line creates a new object from the Customer class; the constructor for the Customer class in this case does not take any arguments. • This is the same as the following lines – where the object is created by a separate assignment: Customer myCustomer1; MyCustomer1 = new Customer(); Comp321: Object Oriented Programming
Arrays • An array is also a reference type, a structure that can hold multiple values – but all the values must be of the same type. That type can be a primitive data type or an object type, or it can be other arrays. • In Java, an array is actually an object – but one that has specialised syntax and behaviour. • An array is declared by specifying the data type of the values it will hold, followed by [] to indicate that it is an array. For example: byte[]arrayOfBytes; //array of values of type byte byte[][]arrayOfArrayOfBytes ; //an array of arrays of byte[] type Customer[]arrayOfCustomers; //array of objects of the class //Customer • The C++ syntax for declaring a variable of array type is also supported by Java i.e. Byte arrayOfBytes[]; //array of values of type byte • However, this syntax can be confusing, so it should be avoided in Java. Comp321: Object Oriented Programming
Arrays(cont) • In the above examples, we can say that byte[] and Customer[] are types. • After an array has been declared, it must be created. Because an array is actually an object in Java, the new keyword must be used to create the array (new is used to create any object). The size of the array i.e. how many elements it can hold must be specified. For example, to declare an array called 'arrayOfBytes' that will hold 1024 bytes and to create it: byte[] arrayOfBytes = newbyte[1024]; • To declare an array of 50 strings: String[] lines = newString[50]; • When the array is created in this way, each of the values in the array is initialised to the corresponding default value for the data type of the value (the default value for an object type is null, meaning the object has an empty reference). • In Java, arrays have a 0-based index, that is, the first element is at index 0. The elements are accessed by the array name followed by the index in square brackets. Comp321: Object Oriented Programming
Arrays(cont) • For example, to declare an array named 'responses', with two elements whose values are 'Yes' and 'No': String[] responses = new String[2]; responses[0] = "Yes"; responses[1] = "No"; //to read the elements System.out.println ("The answer should be " + responses[0] + " or " + responses[1] + "!"); • If the size of an array is n, then the highest index for it is (n-1) e.g. an array of size 5 holds 5 elements, with the last one being accessed at index [4]. • If the code tries to read an element past the end of the array e.g. to read index [5] in an array of size 5, the interpreter throws an ArrayIndexOutOfBoundsException at runtime. Comp321: Object Oriented Programming
Arrays(cont) • The size, or length, of an array can be accessed in code by reading the value of the length property of the array object e.g. intsizeOfResponsesArray = responses.length; • This is commonly used to loop through all the values in an array e.g. int[] valuesArray; //assume this array is created and initialised //somewhere else inttotalValue = 0; //store the sum of the array elements for (inti = 0; i < valuesArray.length; i++) totalValue += valuesArray [i]; • The above example creates an array of values and then uses a for loop to iterate through the values and add them up. As the index for the array is 0-based, the last element is at the index [array.length-1]. • Throwing an exception is part of the runtime error handling of Java. We will look at how to properly handle exceptions later in the course. • The for syntax includes the initialisation, test and update steps for the loop – initialise the counter to 0, loop until i reaches the size of the array, increment i by 1 for each iteration. Comp321: Object Oriented Programming
Assigning Values to Arrays • The null literal can be used to indicate that an object that is of a reference data type does not yet exist i.e. to initialise an object to be empty or null. For example: char[] password = null; //sets the password array of //characters to be null • Literal values can also be used to specify the values of an array, as in the String array example given above: String[] responses = new String[2]; responses[0] = "Yes"; responses[1] = "No"; • An array can also be initialised by the following syntax, where the array object is created and the elements initialised in one statement. The element values are separated by commas. int[] someNumbers = {1, 2, 3, 4} Comp321: Object Oriented Programming
Assigning Values to Arrays(cont) • The new keyword is not used – but the object is implicitly created. • The length of this array is now 4 – again, this is implicit from the specified elements. • The statement above could also be written as: int[] someNumbers = new int[4]; someNumbers[0] = 1; someNumbers[1] = 2; someNumbers[2] = 3; someNumbers[3] = 4; • In fact, the Java compiler compiles the single statement into Java byte codes that are equivalent to the above. The implication of this is that if your program needs to include a large amount of data into an array, it may not be most efficient to include the data literally in the array, as the examples above do. This is because the compiler has to create a lot of Java byte codes to initialise the array, and then the interpreter has to execute all that code. • It may be more efficient to store the data in an external file and read it into the program at runtime. Comp321: Object Oriented Programming
9.Operators • This section covers arithmetic, comparison, conditional and assignment operators. • Java also has a group of operators called shift or bitwise operators – these operate on the individual bits (1s and 0s) of integer values. We will not cover these at this point. Comp321: Object Oriented Programming
9.1. Arithmetic • Java supports the standard arithmetic operators for all integer and floating-point numbers. These are: • + (addition), - (subtraction), * (multiplication), / (division), % (modulo). • All of these are binary operators i.e. they take two operands and the operator appears between the two operands (this is called infix notation), as follows: Operand1 operator operand2 • The subtraction operator can also be used as a unary operator – when it is placed in front of a number e.g. –5. When used in this way, it effectively multiplies the single operand by –1. Comp321: Object Oriented Programming
9.1. Arithmetic(cont) • The addition operator can also be used to concatenate two strings. If either one of the two operands is a string, then the other one is converted to a string and the strings are concatenated. Try the following code in a main method to see the addition operator operating on numbers and on strings. System.out.println (3+4); //prints out 7 System.out.println ("The value is: " + 4); //prints out 'The value is: 4' System.out.println ("The value is: " + 3 + 4); //prints out 'The value is: 34', because there is at least one string operand System.out.println ("The value is: " + (3+4)); //prints out 'The value is 7' – because the parentheses indicate that the sum of 3+4 is evaluated first, resulting in the value 7 Comp321: Object Oriented Programming