Java Programming: H.K.Vedamurthy Asst Professor
Java Programming: H.K.Vedamurthy Asst Professor
Java
Java is related to C++, which is a direct descendant of C.
Much of the character of Java is inherited from these two languages.
C- programming Language
C was Invented and first implemented by Dennis Ritchie on a DEC PDP-11 running the UNIX operating system.
C was the result of a development process that started with an older language called BCPL, developed by Martin Richards. BCPL influenced a language called B, invented by Ken Thompson, which led to the development of C in the 1970s. For many years, the de facto standard for C was the one supplied with the UNIX operating system and described in The C Programming Language by Brian Kernighan and Dennis Ritchie
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
OOP
Throughout the history of programming, the increasing complexity of programs has driven the need for better ways to manage that complexity. C++ is a response to that need. By the early1980s,many projects were pushing the Structured Approach past its limits. To solve this problem, a new way to program was invented, called object-oriented programming(OOP).
OOP is a programming methodology that helps organize Complex programs through the use of Inheritance, Encapsulation, and polymorphism.
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
C++
C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell Laboratories in Murray Hill,New Jersey. Stroustrup initially called the new language C with Classes. However,in1983,the name was changed to C++.C++ extends C by adding object-oriented features. Because C++ is built on the foundation of C,it includes all of Cs features,attributes and benefits.
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
In an attempt to find such a Solution that is portable and platform-independent language that could be used to produce code that would run on a variety of CPUs under differing environments. This effort ultimately led to the creation of Java.
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
Output
Output
This is num: 100 The value of num * 2 is 200
Output
x is less than y x now equal to y x now greater than y
*/ class ForTest { public static void main(String args[]) { int x; for(x = 0; x<10; x = x+1) System.out.println("This is x: " + x); } }
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
Output
This is x: 0 This is x: 1 This is x: 2 This is x: 3 This is x: 4 This is x: 5 This is x: 6 This is x: 7 This is x: 8 This is x: 9
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
Integers Types
Name Long int short byte Width 64 32 16 8 Range ,223,372,036,854,775,808 to 9,223,372,036,854,775,807 2,147,483,648 to 2,147,483,647 32,768 to 32,767 128 to 127
Floating-Point Types
Name double float Width 64 32 Approximate Range 64 4.9e324 to 1.8e+308 1.4e045 to 3.4e+038
\\
\r \n \f
Backslash
Carriage return New line (also known as line feed) Form feed
\t
\b
Tab
Backspace
Dynamic Initialization
// Demonstrate dynamic initialization. class DynInit { public static void main(String args[]) { double a = 3.0, b = 4.0;
// c is dynamically initialized double c = Math.sqrt(a * a + b * b);
Automatic Type Promotion in Expressions Examine the following expression: byte a = 40; byte b = 50; byte c = 100; int d = a * b / c;
The result of the intermediate term a*b easily exceeds the range of either of its byte operands. To handle this kind of problem,
Java automatically promotes each byte, short, or char operand to int when evaluating an expression. This means that the sub expression a*b is performed using H.K.Vedamurthy,Asst Professor,Dept integersnot bytes. The result is 2000. of CSE,SIT Tumkur
Arrays
// Demonstrate a one-dimensional array. class Array {
public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30; month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[7] = 31 month_days[8] = 30; month_days[9] = 31; month_days[10] = 30; month_days[11] = 31; } } System.out.println("April has " + month_days[3] + " days.");
Arrays
Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated
Multidimensional Arrays
// Demonstrate a two-dimensional array.
class TwoDArray { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur }
Output:
01234 56789 10 11 12 13 14 15 16 17 18 19
Jagged Arrays
// Manually allocate differing size second for(i=0; i<4; i++) { dimensions. for(j=0; j<i+1; j++) class TwoDAgain { System.out.print(twoD[i][j] + " "); public static void System.out.println(); main(String args[]) { } int twoD[][] = new } int[4][]; } twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; }
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
Output:
0 12 345 6789
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
output:
The String type is used to declare string variables. You can also declare arrays of strings. A quoted string constant can be assigned to a String variable. A variable of type String can be assigned to another variable of type String. You can use an object of type String as an argument to println( ).
String str = "this is a test"; System.out.println(str);
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
Output
Value is: 1 Value is: 2 Value is: 3 Value is: 4 Value is: 5 Value is: 6 Value is: 7 Value is: 8 Value is: 9 Value is: 10 Summation: 55
H.K.Vedamurthy,Asst Professor,Dept of CSE,SIT Tumkur
Output
Value is: 1 Value is: 2 Value is: 3 Value is: 4 Value is: 5 Value is: 2 Value is: 4 Value is: 6 Value is: 8 Value is: 10 Value is: 3 Value is: 6 Value is: 9 Value is: 12 Value is: 15 Summation: 90