Java Programming Concepts: A Comprehensive Guide
Classified in Computers
Written at on English with a size of 4.47 KB.
- A class is a template used to create objects and declare data types and methods in a program.
- Public - Modifier used to declare classes and interfaces. Void - Method that returns no value. Main - Marks the start and end of program execution.
- Scanner class - Used to get user input for types like String and Int.
- Exceptions: Try-catch block, throw statement.
- Inheritance provides a powerful and natural mechanism for organizing and structuring software programs. It improves clarity in the design of classes.
- A subclass provides specialized behavior based on elements provided by the superclass. Using inheritance, programmers can reuse the code in the superclass many times.
- Reading and Writing to Files:
- Import java.io file.
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");
//outFile.delete(); // Delete output.txt
- TCP - Connection-oriented and reliable for HTTP, FTP, and Telnet. UDP - Connectionless and unreliable for SNMP and DNS.
- Port Numbers: FTP - 21; Telnet - 23; POP3 - 110; SMTP - 25; HTTP - 80; Time service - 13.
- A socket is one endpoint of a two-way communication link between two programs running on the network.
- A thread is a program unit that is executed independently of other parts of the program.
- Multithreading programming is the use of multiple threads in a single program, running at the same time and performing different tasks.
- Thread Scheduler - The thread scheduler runs each thread for a short amount of time called a time slice. Then, the scheduler picks another thread from those that are runnable.
- Concurrency is the execution of multiple tasks at the same time, regardless of the number of processors.
- Declaration for calling sleep method:
InterruptedException
. - TCP Server Socket and Listening for a Connection:
ServerSocket ss = new ServerSocket(PORT_NUMBER); Socket sock = ss.accept();
- Method to Find Port Number:
int port = receivePacket.getPort();
Example GUI Template (Change Bold Text Accordingly)
public TextExample() {
text = new JTextField(20);
setLayout(new FlowLayout());
add(text);
}
Network Code Question
getHostName()
to find cisco.com
host.
Concurrency
Concurrency is the execution of multiple tasks at the same time, regardless of the number of processors.
A Concurrent System is one in which:
- Multiple tasks can be executed at the same time.
- The tasks may be duplicates of each other or distinct tasks.
- The overall time to perform the series of tasks is reduced.
Advantages of Concurrency
- Concurrent processes can reduce code duplication.
- The overall runtime of the algorithm can be significantly reduced.
- More real-world problems can be solved than with sequential algorithms alone.
- Redundancy can make systems more reliable.
Disadvantages of Concurrency
- Runtime is not always reduced, so careful planning and task scheduling are required.
- Concurrent algorithms can be more complex than sequential algorithms.
- Shared data can be corrupted and deadlocked.
- Communication between tasks is needed.
Throw and Catch
- You throw an exception to notify some other part of the program that an exceptional condition has occurred.
- You catch an exception to handle an exceptional situation that has occurred elsewhere in the program.
- If an exception has no matching
catch
clause in the same method, the search for a matchingcatch
clause continues in the calling method. If the entire program has no matchingcatch
clause, then the program terminates.
Asymmetric Encryption
Asymmetric (or "public key") encryption is a much more complex process than symmetric encryption and carries a large processing overhead for the machines concerned.