This assignment delves into essential Java programming concepts. Students will learn about various data types, string manipulation, common programming errors (like logical errors), the difference between compilers and interpreters, and how these components contribute to Java's functionality.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
1.int – this is a primitive data type which can store a number in 32 bits, i.e. the size of this data type is 4 bytes. Therefore, the minimum value that can be stored is -231and the maximum number that can be stored is 231– 1. Any arithmetic calculations which are performed on the integer always gives the result in an integer. Long – this data type can store any number in 64 bits, i.e. the size is 8 bytes. And hence, the minimum number to be stored is -263and maximum number is 263. It is used when we need a wider range of integers to be stored in memory. Short – this data type is smaller than integer as it can store up to 16bits only. The size results in 2 bytes. It is used to deal with small numbers to save memory space. Float – this data type stores single precision 32 bit IEEE 754 floating point numbers. It is used to save memory when a large number of floating point numbers are used. Double – It is double precision 64 bit IEEE 754 floating point. Its range of values is vast and therefore, is recommended not to use to store very precise values. Char – It stores a single 16 bit Unicode character. Characters in java are stored in the Unicode format ‘\u’. The minimum value that can be stored is 0 i.e. ‘\u0000’ and maximum of 65535, i.e. ‘\uffff’. Byte – Byte is the shortest data type of the data types available in java. It stores the value in 8 bits which has the minimum value as -128 and maximum as 127. The problem of this data types with arithmetic calculations are: 1.The modulus operator (%) works only with integer data type and result in int. It is not suitable to work with double, float or any other data type which stores the floating points. 2.The result of division operator for any 2 integers is always the integer and gives the result as the quotient of the operation. It doesn’t consider the remainder left behind. 3.Char data types are only operated by addition or subtractions and result in chars. It doesn’t take other operators for the calculations. 2.Syntax errors – These errors are those which occurs due to not following the standard syntax of the language. They can be determined when the program is compiled. Logic errors – These errors occur when the logic for a program or a part of a program is not correctly implemented. These errors are due to the lack of knowledge or the implementation. They can be determined with a step by step solution of a problem.
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Runtime errors – these errors often occur due to typo mistake. For example to form an infinite loop, or not to increment an iterator, divide by 0 or access an out of bound element. Semantic errors – These errors occur due to an improper and invalid use of java statements, i.e. to use the statements where it is not even valid or is necessary to use. Ex – To use a variable which is not initialized. int a; a++;//semantic error. 3.Values in a primitive data type variable is stored directly in the memory. It will always have a value in it (garbage value if not initialized) but can never be null. Whereas a reference data type variable doesn’t store the value directly but is a handle to the object of the type. It is created in heap memory and can be null, if not initialized. In java, we have an upper case ‘S’ for String whereas lower case ‘s’ for short because the String is a reference data type whereas the short is primitive data type. As discussed, reference data type are the reference to the object, i.e. it is an object which is declared and hence initialized and used of a class. Here, the class name for the object of String type isString. As java uses CamelCase conventions, that’s why the we have an upper case ‘S’ for String. short is a primitive data type whose class isShortnot short but we don’t use the class name to declare the variable of primitive data type as it doesn’t create a reference to the object but is directly accessed. 4. Syntax error – Syntax errors in java are those errors which occurs when a standard syntax (grammar) is not followed in a sequence of characters or a set of tokens while writing a program. Ex – if a semicolon (;) is missed at the end of the statement. Incorrect: System.out.println(“Hello”) Correct:System.out.println(“Hello”); These errors are easy to handle as when the program is compiled and any syntax errors are present in the file, then the compiler shows the list of all the syntax errors that occur in the file and asks the user to correct all the errors. Even if any one error remains, then the compiler doesn’t let the programmer execute the file. Runtime error – Run time errors are those errors which are not detected at the compilation time but occurs when the program is executed. These results in abortion or the malfunctioning of the program. Ex – If a number is divided by 0. Incorrect: a = 30/0; Correct:a = 30/10; These errors are sometimes difficult to handle as they are not easily detected. If the error aborts the program, then the error is easy to find out. But if it malfunctions and
produces a wrong result, then the backtracking is difficult as compared to abortion and can result in a lot of time efforts. Logic error – Logical errors are the errors which are syntactically correct but produce a wrong output due to a wrong implementation of a logic. These errors are hardest to find in a long program. Ex – if a number is multiplied to another number instead of dividing it. Or calling a method with wrong set of parameters. Here, correct and incorrect statements depend on the logic of the program. These errors can only be handled with a little extra focus and concentration while typing the program and testing each module separately in order to check the logic. 5.String – In java, String is class which stores a sequence of characters. When a variable is created then it is an object of the String class. Literal – In java, literal is the syntactic representation of any data type. It is the actual value in the standard form which can be stored in the memory directly or by the reference of an object. Double quotes – As Strings are a sequence of characters which are put together to form a word, phrase or sentence. A character is always denoted by single quotes (‘’). Hence, the strings are denoted by double quotes (“”). Null string – A null string is a string which has no value in it. It is referenced to no value or characters and hence the length of the string is not defined. If the length of a null string is tried to be accessed, then a null pointer exception is raised. No, a null string is not same as empty string because the null string is not referenced to anything but is nullify. However, an empty string is referred to a string where nothing is stored and hence the size of the string becomes 0. If we print an empty string, then nothing will be displayed as it contains an empty string but null string will print null on the string. Numeric string – numeric string is a string which contains only numbers in the string. Every number in the string is treated as character and hence have an associated Unicode with it. Concatenate strings – Concatenate strings means to join two strings into one single string. A string is put after the other string in it. In java, this can simply be done using + operator. Ex – String s = “Hello” + “World”; Here, String s will contain “HelloWorld”. Declaration of strings – There are two ways to declare a string: i)String s = “Hello”; ii)String s = new String(“Hello”);
In both the ways, a new string named s is created and the value contained will be “Hello”. The difference between two is that in first case the string is created using a string literal pool. Every data type has a literal pool in java out of which a variable or an object can be instantiated. For the second case, a reference object is created in the heap memory which is initialized to value “Hello”. 6.Compilers – Compiler, in any programming language, is a program which translates a set of source from the programming language into a executable code for the execution. As the programming language is a high level language, it converts the high level language to a low level language. It converts the whole program at once. Interpreter – Interpreter is also a program same as compilers which converts the program written in any programming language into an executable code but it converts only one line at a time. It doesn’t let the programmer proceed until the previous line is converted. In java, compilers produce a java class file which contains a platform independent bytecode.