Java Sip for the day – 8th October 2019
Increment and Decrement operators
public class Incdec {
public static void main(String[] args) {
int a1 = 5;
--a1; //predecrement
int a2 = 10;
++a2; //preincrement
int b1 = 15;
b1--; //postdecrement
int b2 = 20;
b2++; //postincrement
System.out.println(a1);
System.out.println(a2);
System.out.println(b1);
System.out.println(b2);
}
}
OUTPUT
4
11
14
21
Java Sip for the day – 7th October 2019
Java program to perform basic operations
public class Operator{ public static void main(String[] args) { int a = 10; int b = 5; int add = a+b; int sub = a-b; int mul = a*b; int div = a/b; int mod = a%b; System.out.println(add); System.out.println(sub); System.out.println(mul); System.out.println(div); System.out.println(mod); } }
Java Sip for the day – 6th October 2019
Simple Java Program
class Program{ public static void main(String[ ] args){ System.out.println("Java Sip is tasty !"); } }
Java Sip for the day – 4th October 2019
Java Sip for the day – 1st October 2019
Java Sip for the day – 22nd September 2019
Arrays in JAVA
An array is a collection of variables which are of similar type. Arrays can be accessed with their indexes.
Arrays can be used to store numbers, list of values and other variables.
Consider the following example below :
int[ ] Bitcoin = new int[5];
Here, we are defining an array Bitcoin of integer datatype consisting of 5 Bitcoins.
We can reference each element in the array with an index, say the elements of the array are {5,50,85,22,78}. We can access the 2nd element by using Bitcoin[1]. Note indexing in Java starts at 0.
Let us look into a program to access arrays by referencing:
public class Cryptocurrency {
public static void main(String[] args) {
String[ ] Blockchain = { “Amsterdam”, “Berlin”, “Capetown”, “Delhi”};
System.out.println(Blockchain[2]);
}
}
Program to calculate length of an array.
public class Coins {
public static void main(String[] args) {
int[ ] Bitcoin = new int[5];
System.out.println(Bitcoin.length);
}
}
Program to find sum of an array.
public class Coins {
public static void main(String[] args) {
int [ ] Bitcoin = {5,50,85,22,78};
int sum=0;
for(int k=0; k<Bitcoin.length; k++) {
sum += Bitcoin[k];
}
System.out.println(sum);
}
}
In this code, the variable sum stores all the variables by adding all the elements of array. Initially the sum is assigned a value of zero. A for loop traverses through the array list because of the expression k<Bitcoin.length, that is the number of elements will be equivalent to a value less than length of the array. Remember, the indexing starts at [0] for arrays.
Printing values of an Array.
public class Coins {
public static void main(String[] args) {
int[ ] values = {5,50,85,22,78};
for (int P: values) {
System.out.println(P);
}
}
}
2D and 3D Arrays
Accessing 2D Array
public class Coins {
public static void main(String[] args) {
int[ ][ ] Bitpart = { {77, 55, 34}, {41, 54, 63} };
int j = Bitpart[1][0];
System.out.println(j);
}
}
Accessing 3D Array
public class Coins {
public static void main(String[] args) {
int[ ][ ] Bitpart = { {17, 21, 32}, {41,70,44}, {51, 62, 75} };
Bitpart[0][2] = 32;
int m = Bitpart[1][0];
System.out.println(m);
}
}
Similarly, N dimensional arrays can also be accessed.
Source:1)THE Java™ Programming Language, Fourth Edition By Ken Arnold, James Gosling, David Holmes 2) SoloLearn Online Platform.
Difference between Process and Thread in Java
Process | Thread |
---|---|
1) Part of Concurrent Programming. | 1) Integral part of Concurrent Programming. |
2) A process has a self-contained execution environment. | 2) A thread is a lightweight process. |
3) Process has its own memory space. | 3) Threads share the process's resources, including memory and open files. |
4) Processes are synonymous with programs or applications. | 4) Threads do things such as memory management and signal handling. |
5) A Java application can create additional processes using a ProcessBuilder object. | 5) A Main thread has the ability to create additional threads. |
6) To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources, such as pipes and sockets. | 6) Threads exist within a process. This makes for efficient, but potentially problematic, communication. |
Processing time for a single core is shared among processes and threads through an OS feature called time slicing.
Time Slice or Preemption : A technique to implement multitasking in operating systems. The period of time for which a process is allowed to run in a preemptive multitasking system is generally called the time slice or quantum. The scheduler is run once every time slice to choose the next process to run.
Source: 1) The Java™ Tutorials 2) Wikipedia
Java Collection Framework
A collection or container is an object that groups multiple elements into a single unit, they are used to store, retrieve, manipulate and aggregate data.
Collections Framework
A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:
- Interfaces: Interfaces are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
- Implementations: These are the concrete implementations of the collection interfaces. Implementations are reusable data structures.
- Algorithms: Algorithms are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. Algorithms possess reusable functionality.
Interfaces
The collection interfaces encapsulate different types of collections. These interfaces allow collections to be manipulated independently of the details of their representation.
- Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn’t provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such as Set and List.
- Set — a collection that cannot contain duplicate elements.
- SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.
- List — an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).
- Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
Queues usually order elements in a FIFO (first-in, first-out) manner.
- Deque — Similar to queue but Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends.
- Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value.
- SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.
If an unsupported operation is invoked, a collection throws an UnsupportedOperationException.
Note: All core collection interfaces are generic.
Source: The Java™ Tutorials