Download Java Programming: Variables, Control Structures, and Arrays and more Lecture notes Java Programming in PDF only on Docsity!
Introduction to java
How are Java programs written?
How are variables declared?
What are naming conventions in java?
How are expressions specified?
How are control structures defined?
Conditional structure(if/else)
Iteration structures (loops)
The selective structure(switch)
Public keyword
Public is a keyword
Keywords :reserved words which have specific
meaning relevant to a compiler in java
programming language
Public keyword indicates the following :
-- It is used as an access control modifier.
-- It is applicable to a class or a method.
-- public class, method or a variable are
visible to all the classes defined in different
package.
public class HelloWorld
Main
public static void main (String[] args)
1. public - declares that the main method is publicly
accessible to other classes
2. static - declares that the main method can be
invoked without creating an instance of the class
3. void - declares that the main method does not return
any value.
4. main - defines the name of the method 5. String[]
args - defines a parameter to the main method which
will contain any command line options passed by the
user when invoking the progam. These command line
options will be passed to the program as an array of
string objects.
Variables Declaration
int myInt; /* Declaring an uninitialized
variable called 'myInt', of type 'int' */
myInt = 35; // Initializing the variable
int myInt = 35; // Declaring and initializing
the variable at the same time
int a, b; // Declaring multiple variable of the
same type
int a = 2, b = 3; // Declaring and initializing
multiple variables of the same type
Example : Fibonacci Series
Fibonacci:
class Fibonacci {
public static void main(String[] arg) {
int lo = 1;
int hi = 1;
System.out.println(lo);
while (hi < 50) {
System.out.println(hi);
hi = lo + hi;
lo = hi – lo;
}
}
}
8
Arrays in Java
Declaration:
int[] array_name; //declares an array of integers
String[] names; //declares an array of string
int[][] matrix; //this is an array of arrays(2d array)
Memory Allocation:
int[] num= {100, 200, 300, 400, 500, 600};
lengthOfArray= num.length;
int[] array_name = new int[5]; // array of
integers of size 5
Arrays
Similarly, you can declare arrays of other types:
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;
Switch
class SwitchDemo {
public static void main(String[] args)
{ int month = 8;
switch (month)
{ case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Invalid month.");break; }
OUTPUT: In this case, "August" is printed to standard output.
When to use switch and when
if/else….??
…
Operators
Assignment Operator: int cadence = 0;
The Arithmetic Operators
+ additive operator (also used for String
concatenation)
- subtraction operator
* multiplication operator
/ division operator
% remainder operator
Example Arithmetic operators
class ArithmeticDemo {
public static void main (String[] args){
int result = 1 + 2; // result is now 3 System.out.println(result);
result = result - 1; // result is now 2 System.out.println(result);
result = result * 2; // result is now 4 System.out.println(result);
result = result / 2; // result is now 2 System.out.println(result);
result = result + 8; // result is now 10
result = result % 7; // result is now 3
System.out.println(result); } }
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example,
x+=1; and
x=x+1; both increment the value of x by 1.
The Unary Operators
The unary operators require only one operand; they
perform various operations such as
incrementing/decrementing a value by one, negating
an expression, or inverting the value of a boolean.
+ Unary plus operator; indicates positive value
(numbers are positive without this, however)
- Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a
boolean
Unary Operator Example
class UnaryDemo {
public static void main(String[] args){
int result = +1; // result is now 1
System.out.println(result);
result--; // result is now 0
System.out.println(result);
result++; // result is now 1
System.out.println(result);
result = -result; // result is now -
System.out.println(result);
boolean success = false;
System.out.println(success); // false
System.out.println(!success); // true } }