Below you will find pages that utilize the taxonomy term “java”
Posts
Safely Indexing an Array (C to Java)
Arrays
Arrays are contiguous locations of memory set aside during compilation. (memory allocated is virtual address which is later mapped to actual memory address during run-time)
In the most simple form you need a base address and you can read starting from there. (there’s a limit how far you can go even with languages like C)
Simple Integer Array in C:
int arr[] = {1,2}; for (int i=0;i<10;i++){ printf("%d\n",arr[i]); } C doesn’t have a way to have a check on how far to go.
read morePosts
Good Object Modelling
OOP is all about Encapsulation Yes, that’s right. Object Oriented Programming aims to encapsulate properties and methods into a consolidated object so that’s operations can be carried out on the object. The whole aim was to move from procedural functions which were not easy to reason about and prove correctness. But this principle often gets violated and people write procedural code using objects. Classic Example is:
class Rectangle { private Long length; private Long breadth; //gettters //setters //constructors } And caller calculates area by:
read morePosts
A tiny Springboot app
This miniblog is adpated from this linkedin course
The tweet that started it all Install springboot cli Easiest way is via sdkman.
sdk install springboot Groovy classFile:
ThisClassWillActuallyRun.groovy
@Controller class ThisWillActuallyRun { @RequestMapping("/") @ResponseBody String home() { "Hello World!" } } Running the app:
spring ThisWillActuallyRun Output:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .
read morePosts
Some notes on gradle
Java plugin for gradle Add build.gradle at root of the project
apply plugin: 'java' This links the gradle. Makes gradle aware of java.
build.gradle is written in groovy Intellij comes with an inbuilt groovy console (interpreter for the language)
Groovy console can also be used as ‘java interpreter’ to quickly evaluate java code and api (akin to jshell)
groovy closures:
class Myclass{ void doSomething(Closure closure){ closure.call() } } obj = new Myclass() obj.
read morePosts
SDKMAN, Java9, Jshell, REPL
Working with multiple versions
Most of the times we as developers are working on multiple version of the language or the framework. If not managed properly it can be a nightmare dealing with different projects which use different versions of the same language. Say, project A uses python2 and project B uses python3. In python we have virtual environments which can be managed using, among many others, pipenv. Likewise for Ruby we have rbenv.
read morePosts
Working with Spring Data and Json
1. Overview
Json is, in many places, the preferred way of storing data due to it’s brevity (mostly compared to xml) and human readability. While working in any web framework which involve operation with data models and templates, usage of json data format is almost inevitable. Languages like Python, Ruby even have this format conceptually built into the core language library itself. Hash in ruby and dictionaries in python which allow json-(dictionary/hash) conversion with an ease.
read morePosts
Equals and HashCode in Java
The parent of all ‘Object’ classes in Java has, among many, two notable methods: equals and hashCode. For the most of the part, we can go about without tinkering/overriding these methods, but they play a significant role when we are dealing with any kind of data structure whose implementation depends on hashing. Maps, Sets, and their various derivatives and implementations of the Java Collection framework depend on the hashing of objects.
read morePosts
Leveraging Lombok Annotations to spice up Java
Java is heavily criticized and hated for its verbosity. Even for things such as reading/writing files we have to ceremoniously write a lot of generic stuff. The increasingly popular Java library Lombok has been hugely successful in addressing this issue. It’s an annotation based Java library that plugs itself into editors, IDE, and build tools at compile time, facilitating the generation of boilerplate code and making the code clean and more readable.
read more