Advanced java concepts with examples .
Answers
Answered by
4
Open topic with navigation
Advanced Java Concepts
Object Oriented Programming
It is common to approach writing code as a sequence of steps that you must perform in order to achieve some objective. A consequence of this approach is that quite often extremely long, complicated functions are created that are very similar to other long and complicated functions. Whilst you can solve the problem of duplication by creating additional functions, it is better overall to adopt a different mindset when writing code and this is called "Object Oriented" or "OO" programming. Essentially OO involves "abstracting" or breaking up your code into separate classes for each role or object in your problem, and then concentrate on writing appropriate functions for each of these objects specifically. Your program flow then goes from object to object rather than being all contained in the one class. To assist in breaking your problem into objects, it is extremely useful to have a thorough understanding of "design patterns". Design patterns are essentially common problems and the ways that they are best represented by objects.
Synchronization
To prevent two different threads from accessing a class simultaneously and overwriting each others state, you can define that your functions or sections of code are "synchronized". This means that only one thread can call this function or section of code at a time, and a second thread pauses until the first is finished before it proceeds. You can additionally synchronize functions or sections of code on objects or locks. This can be any object (including "this", the object is accessed) and can mean that you can coordinate together different functions and sections of code.
Static variables
You can use static variables to simplify the interaction between multiple java exits, however can also be extremely dangerous if misunderstood. Static variables are contained in the class definition as opposed to being contained in the class instances. This means that if you define a variable as static, there is only ONE of them, and it is SHARED amongst all of the individual class instances. Additionally, if this variable is made public, or is retrieved by a public static function, then this variable is accessible to any piece of code anywhere.
For example, if you have a single VtiCustomSerialPortExit that continuously reads values in from a weighbridge, it could write this value into a static variable. You can access this variable by a VtiUserExit to display the most recent value on the screen. This works well, however, if you have two weighbridges connected to your server and create two separate instances of the same VtiCustomSerialPortExit, you immediately run into trouble. The single static variable is updated continuously from both weighbridges and the VtiUserExits are not able to know which weighbridge the variable is from. This new situation is best resolved using a separate weighbridge manager singleton class that stores a list of the weighbridge values paired with a corresponding name so that it can be requested by the VtiUserExits.
Singleton
Singleton is a design pattern that specifies a method to create a single statically accessible class. This is quite often used for manager classes or whenever you want to ensure that there is only ever one instance of this class instantiated. The use of this design pattern is much more preferable to create a class that consists solely of static variables and functions for two reasons. It gives you control over the instances life cycle, and it also allows you to easily upgrade to allow multiple instances in the future if necessary.
JNI
The Java Native Interface (JNI) allows java code to call native C++, C or assembler functions. This is extremely useful specifically for calling faster performing code, or for accessing system functionality that is not available from within Java. However, writing JNI is non-trivial and small mistakes can cause the JVM to crash and /or be extremely difficult to track down. It is best to stick to passing primitive types if possible, as the interfacing of java objects in the JVM from native code gets complicated very quickly. It is possible though, to do most things that you .
JNI Example 1
Package com.company.project;
Class MyJniClass {
Public static native double performLongCalculation(double value);
Static {
System.loadLibrary("MyJniLib");
}
}
The C++ code must follow a specific structure, so it is best to generate the header file automatically using "javah".
JNI Example 2
javah -jni -o MyJniLib.h -classpath . com.company.project.MyJniClass
Then you can create your source file from the generated header.
JNI Example 3
#include <jni.h>
#include "MyJniLib.h"
#include <stdio.h>
JNIEXPORT jdouble JNICALL Java_com_company_project_MyJniLib_performLongCalculation(
JNIEnv *env, jobject obj, jdouble value)
{
jdouble ret = value;
// Perform the long calculation...
return ret;
}
plzz mark this as brainliest answer
Advanced Java Concepts
Object Oriented Programming
It is common to approach writing code as a sequence of steps that you must perform in order to achieve some objective. A consequence of this approach is that quite often extremely long, complicated functions are created that are very similar to other long and complicated functions. Whilst you can solve the problem of duplication by creating additional functions, it is better overall to adopt a different mindset when writing code and this is called "Object Oriented" or "OO" programming. Essentially OO involves "abstracting" or breaking up your code into separate classes for each role or object in your problem, and then concentrate on writing appropriate functions for each of these objects specifically. Your program flow then goes from object to object rather than being all contained in the one class. To assist in breaking your problem into objects, it is extremely useful to have a thorough understanding of "design patterns". Design patterns are essentially common problems and the ways that they are best represented by objects.
Synchronization
To prevent two different threads from accessing a class simultaneously and overwriting each others state, you can define that your functions or sections of code are "synchronized". This means that only one thread can call this function or section of code at a time, and a second thread pauses until the first is finished before it proceeds. You can additionally synchronize functions or sections of code on objects or locks. This can be any object (including "this", the object is accessed) and can mean that you can coordinate together different functions and sections of code.
Static variables
You can use static variables to simplify the interaction between multiple java exits, however can also be extremely dangerous if misunderstood. Static variables are contained in the class definition as opposed to being contained in the class instances. This means that if you define a variable as static, there is only ONE of them, and it is SHARED amongst all of the individual class instances. Additionally, if this variable is made public, or is retrieved by a public static function, then this variable is accessible to any piece of code anywhere.
For example, if you have a single VtiCustomSerialPortExit that continuously reads values in from a weighbridge, it could write this value into a static variable. You can access this variable by a VtiUserExit to display the most recent value on the screen. This works well, however, if you have two weighbridges connected to your server and create two separate instances of the same VtiCustomSerialPortExit, you immediately run into trouble. The single static variable is updated continuously from both weighbridges and the VtiUserExits are not able to know which weighbridge the variable is from. This new situation is best resolved using a separate weighbridge manager singleton class that stores a list of the weighbridge values paired with a corresponding name so that it can be requested by the VtiUserExits.
Singleton
Singleton is a design pattern that specifies a method to create a single statically accessible class. This is quite often used for manager classes or whenever you want to ensure that there is only ever one instance of this class instantiated. The use of this design pattern is much more preferable to create a class that consists solely of static variables and functions for two reasons. It gives you control over the instances life cycle, and it also allows you to easily upgrade to allow multiple instances in the future if necessary.
JNI
The Java Native Interface (JNI) allows java code to call native C++, C or assembler functions. This is extremely useful specifically for calling faster performing code, or for accessing system functionality that is not available from within Java. However, writing JNI is non-trivial and small mistakes can cause the JVM to crash and /or be extremely difficult to track down. It is best to stick to passing primitive types if possible, as the interfacing of java objects in the JVM from native code gets complicated very quickly. It is possible though, to do most things that you .
JNI Example 1
Package com.company.project;
Class MyJniClass {
Public static native double performLongCalculation(double value);
Static {
System.loadLibrary("MyJniLib");
}
}
The C++ code must follow a specific structure, so it is best to generate the header file automatically using "javah".
JNI Example 2
javah -jni -o MyJniLib.h -classpath . com.company.project.MyJniClass
Then you can create your source file from the generated header.
JNI Example 3
#include <jni.h>
#include "MyJniLib.h"
#include <stdio.h>
JNIEXPORT jdouble JNICALL Java_com_company_project_MyJniLib_performLongCalculation(
JNIEnv *env, jobject obj, jdouble value)
{
jdouble ret = value;
// Perform the long calculation...
return ret;
}
plzz mark this as brainliest answer
Similar questions
Math,
7 months ago
Math,
7 months ago
Biology,
7 months ago
Social Sciences,
1 year ago
Physics,
1 year ago