Programming Tips πŸ’‘
51.5K subscribers
67 photos
10 videos
30 files
354 links
Programming & AI:
Tips πŸ’‘
Articles πŸ“•
Resources πŸ‘Ύ
Design Patterns πŸ’Ž
Software Principles βœ…

πŸ‡³πŸ‡± Contact: @MoienTajik

🎯 Buy ads: https://telega.io/c/ProgrammingTip
Download Telegram
Use Strings carefully ⚠️

If two Strings are concatenated using β€œ+” operator in a β€œfor” loop, then it creates a new String Object, every time.

This causes wastage of Memory and increases Performance time.

Also, while instantiating a String Object, constructors should be avoided and instantiation should happen directly. For example:

//Slower Instantiation ❌
String bad = new String("string object");

//Faster Instantiation βœ…
String good = "string object";


#memory #java #string
@ProgrammingTip
Avoiding Memory Leaks By Simple Tricks βœ…

Memory Leaks often cause performance degradation of software. Since, Java manages memory automatically, the developers do not have much control.

But there are still some standard practices which can be used to protect from memory leakages. πŸ™†πŸ»β€β™‚οΈ

1️⃣ - Always release database connections when querying is complete.

2️⃣ - Try to use Finally block as often possible.

3️⃣ - Release instances stored in Static Tables.

#java #memory #performance
@programmingTip
Measuring Time πŸ•°

Many applications require a very precise time measurement. For this purpose, Java provides static methods in System class. πŸ”†

1️⃣ - currentTimeMillis(): Returns current time in MilliSeconds since Epoch Time, in Long.
view sourceprint?

long startTime = System.currentTimeMillis();
long estimatedTime = System.currentTimeMillis() - startTime;


2️⃣ - nanoTime(): Returns the current value of the most precise available system timer, in NanoSeconds, in long.

nanoTime() is meant for measuring relative time interval instead of providing absolute timing.

long startTime = System.nanoTime();
long estimatedTime = System.nanoTime() - startTime;


#java #time
@ProgrammingTip
πŸ–Ό Rescale Image In Java πŸ–Ό

An image can rescaled using AffineTransform. First of all, Image Buffer of input image is created and then scaled image is rendered. βœ”οΈ

BufferedImage imgSource = 
ImageIO.read(new File("myImage.jpg"));

BufferedImage imgDest =
new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = imgDest.createGraphics();

AffineTransform affinetransformation =
AffineTransform.getScaleInstance(2, 2);

g2d.drawRenderedImage(imgSource,
affinetransformation);

ImageIO.write(imgDest, "JPG",
new File("outImage.jpg"));


#java #image
@ProgrammingTip
πŸ“¦ Grab The Stack Trace Of An Exception πŸ“¦

Checking your code for errors is one of the most Painstaking – and frustrating – parts of the development process. πŸ™†πŸ»β€β™‚οΈ

It’s also horrendously Time-Consuming, particularly if you’ve a pesky error whose source isn’t immediately clear. πŸ’‘

Using this code, you can more easily track down where an Exception is occurring – and bring yourself that much closer to being rid of it. βœ…

Code
Exception e = …;
java.io.StringWriter sw = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(sw));
String trace = sw.getBuffer().toString();


#java #exception #error #trick
@ProgrammingTip
Use String.valueOf() instead of toString() πŸ’Ž

If obj needs to be converted to string then the result of obj.toString() and String.valueOf(obj) will be same.

But String.valueOf() is Null Safe, means it will never throw NullPointerException. 🎯

Test test = null;

// Below statement will not throw NPE
System.out.println(String.valueOf(test));

// Next statement will throw NPE
System.out.println(test.toString())

γ€°γ€°γ€°γ€°γ€°γ€°
#java #string
@ProgrammingTip